Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Data Manipulation > Statistics > Random Number Generation >
Mathematica > Mathematics and Algorithms > Mathematical Functions > Random Number Generation >
Mathematica > Mathematics and Algorithms > Statistics > Random Number Generation >

RandomSample

RandomSample[{e1, e2, ...}, n]
gives a pseudorandom sample of n of the e_i.
RandomSample[{w1, w2, ...}->{e1, e2, ...}, n]
gives a pseudorandom sample of n of the e_i chosen using weights w_i.
RandomSample[{e1, e2, ...}]
gives a pseudorandom permutation of the e_i.
  • RandomSample[{e1, e2, ...}, n] never samples any of the e_i more than once.
  • RandomSample[{e1, e2, ...}, n] samples each of the e_i with equal probability.
  • RandomSample gives a different sequence of pseudorandom choices whenever you run Mathematica. You can start with a particular seed using SeedRandom.
  • A Method option to SeedRandom can be given to specify the pseudorandom generator used.
Find a sample in which no elements ever occur more than once:
Generate a random permutation:
Find a sample in which no elements ever occur more than once:
In[1]:=
Click for copyable input
Out[1]=
 
Generate a random permutation:
In[1]:=
Click for copyable input
Out[1]=
The elements can be any expressions:
With larger weights for later elements, later elements tend to be selected for the sample:
Successively fill in black squares down the page:
Use SeedRandom to get repeatable random values:
Use BlockRandom to block one use of RandomSample from affecting others:
RandomSample of size 1 follows the same distribution as RandomChoice:
If the sample size is larger than the population size an error message is returned:
New in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team