RandomSample[{e1,e2,…},n]
gives a pseudorandom sample of n of the ei.
RandomSample[{w1,w2,…}{e1,e2,…},n]
gives a pseudorandom sample of n of the ei chosen using weights wi.
RandomSample[{e1,e2,…}]
gives a pseudorandom permutation of the ei.
RandomSample
RandomSample[{e1,e2,…},n]
gives a pseudorandom sample of n of the ei.
RandomSample[{w1,w2,…}{e1,e2,…},n]
gives a pseudorandom sample of n of the ei chosen using weights wi.
RandomSample[{e1,e2,…}]
gives a pseudorandom permutation of the ei.
Details
- RandomSample is also known as simple random sampling or sampling without replacement.
- RandomSample[{e1,e2,…},n] never samples any of the
more than once. - RandomSample[{e1,e2,…},n] samples each of the
with equal probability. - RandomSample[{e1,e2,…},UpTo[n]] gives a sample of n of the ei, or as many as are available.
- RandomSample[i;;j;;k,n] may be used to sample the Span from i to j in steps of k.
- RandomSample gives a different sequence of pseudorandom choices whenever you run the Wolfram Language. You can start with a particular seed using SeedRandom.
- A Method option to SeedRandom can be given to specify the pseudorandom generator used.
Examples
open all close allBasic Examples (3)
Find a sample in which no elements ever occur more than once:
RandomSample[Range[30], 20]Generate a random permutation:
RandomSample[Range[10]]Generate a random sample of 6 elements, or as many as there are if fewer:
RandomSample[Range[5], UpTo[6]]Scope (4)
The elements can be any expressions:
RandomSample[{Exp[x], x ^ 2, 1 / x, 2}]With larger weights for later elements, later elements tend to be selected for the sample:
RandomSample[Range[30] -> Range[30], 10]Sample over all numbers between -10 and 10:
RandomSample[-10 ;; 10, 10]Sample over even numbers between -10 and 10:
RandomSample[-10 ;; 10 ;; 2, 10]Guarantee that a set of random integers over a big range has no repetitions:
RandomSample[1 ;; 2 ^ 30, 10]Applications (1)
Properties & Relations (3)
Use SeedRandom to get repeatable random values:
{RandomSample[Range[10]], RandomSample[Range[10]]}{SeedRandom[1234];RandomSample[Range[10]], SeedRandom[1234];RandomSample[Range[10]]}Use BlockRandom to block one use of RandomSample from affecting others:
{BlockRandom[RandomSample[Range[10]]], RandomSample[Range[10]]}RandomSample of size 1 follows the same distribution as RandomChoice:
Table[RandomSample[{a, b, c, d}, 1], {10 ^ 5}]//Flatten//Tally//SortRandomChoice[{a, b, c, d}, 10 ^ 5]//Tally//SortTable[RandomSample[10 ^ Range[4] -> {a, b, c, d}, 1], {10 ^ 5}]//Flatten//Tally//SortRandomChoice[10 ^ Range[4] -> {a, b, c, d}, 10 ^ 5]//Tally//SortSee Also
RandomChoice RandomInteger SeedRandom BlockRandom Permutations Subsets Span Downsample
Function Repository: BootstrappedEstimate Shuffle
Tech Notes
Related Guides
Text
Wolfram Research (2007), RandomSample, Wolfram Language function, https://reference.wolfram.com/language/ref/RandomSample.html (updated 2015).
CMS
Wolfram Language. 2007. "RandomSample." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/RandomSample.html.
APA
Wolfram Language. (2007). RandomSample. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/RandomSample.html
BibTeX
@misc{reference.wolfram_2026_randomsample, author="Wolfram Research", title="{RandomSample}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/RandomSample.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_randomsample, organization={Wolfram Research}, title={RandomSample}, year={2015}, url={https://reference.wolfram.com/language/ref/RandomSample.html}, note=[Accessed: 15-June-2026]}