Random sampling from lists.

Here is a way to create a function to take a random sample from a list either with replacement or without replacement.

With replacement.

Suppose we are given a list of elements and we wish to take a random sample from the list assuming we have replacement. (An element can be selected more than once.) We will use the function Random to do this.

RandomSampleWR[lis_List, num_] := 
      Module[{len}, 
len  =  
          Length[lis]; 
newlis  = 
          Table[Part[lis, 
              Random[Integer, {1, 
                  len}]], {num}]

]

Here is a list.

lis  =  CharacterRange["a", "t"]

RandomSampleWR[lis, 7]

Without replacement

Here we need to work a little harder to make sure we do not select an element of our list twice. We do this by dropping the selected element from our list as we choose it and then taking the complement of the original list with respect to our deleted list.

RandomSampleWO[lis_List, num_] := 
      Module[{len, selectfunc, ll, n, aa}, 
len  =  
          Length[lis]; 
selectfunc[{ll_, 
              n_}] := 
{Drop[
              ll, {aa = Random[Integer, {1, Length[ll]}], aa}], 
            n - 1}; 
Complement[lis, 
          First[Nest[selectfunc[#] &, {lis, num}, 
              num]]]
]

samp  = RandomSampleWO[lis, 5]