({{, (RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[e, 1]),(Subscript[e, 2]),…},n] 
gives a pseudorandom sample of n of the (Subscript[e, i]).}, {, (RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[w, 1]),(Subscript[w, 2]),…}->{(Subscript[e, 1]),(Subscript[e, 2]),…},n] 
gives a pseudorandom sample of n of the (Subscript[e, i]) chosen using weights (Subscript[w, i]).}, {, (RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[e, 1]),(Subscript[e, 2]),…}] 
gives a pseudorandom permutation of the (Subscript[e, i]).}})

OpenersNotesSection(Spacer1)Details

(RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[e, 1]),(Subscript[e, 2]),…},n] never samples any of the (Subscript[e, i]) more than once.

(RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[e, 1]),(Subscript[e, 2]),…},n] samples each of the (Subscript[e, i]) with equal probability.

(RefLink[RandomSample, paclet:ref/RandomSample])[{(Subscript[e, 1]),(Subscript[e, 2]),…},(RefLink[UpTo, paclet:ref/UpTo])[n]] gives a sample of n of the (Subscript[e, i]), or as many as are available.

(RefLink[RandomSample, paclet:ref/RandomSample])[i;;j;;k,n] may be used to sample the (RefLink[Span, paclet:ref/Span]) from i to j in steps of k.

(RefLink[RandomSample, paclet:ref/RandomSample]) gives a different sequence of pseudorandom choices whenever you run the Wolfram Language. You can start with a particular seed using (RefLink[SeedRandom, paclet:ref/SeedRandom]).

A (RefLink[Method, paclet:ref/Method]) option to (RefLink[SeedRandom, paclet:ref/SeedRandom]) can be given to specify the pseudorandom generator used.

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic 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]]

OpenersExampleSection1(Spacer1)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]

OpenersExampleSection3(Spacer1)Applications  (1)

Successively fill in black squares down the page:

ArrayPlot[FoldList[ReplacePart[#,#2->1]&,Table[0,{30}],RandomSample[Range[30],30]]]

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

Use (RefLink[SeedRandom, paclet:ref/SeedRandom]) to get repeatable random values:

{RandomSample[Range[10]],RandomSample[Range[10]]}

{SeedRandom[1234];RandomSample[Range[10]],SeedRandom[1234];RandomSample[Range[10]]}

Use (RefLink[BlockRandom, paclet:ref/BlockRandom]) to block one use of (RefLink[RandomSample, paclet:ref/RandomSample]) from affecting others:

{BlockRandom[RandomSample[Range[10]]],RandomSample[Range[10]]}

(RefLink[RandomSample, paclet:ref/RandomSample]) of size 1 follows the same distribution as (RefLink[RandomChoice, paclet:ref/RandomChoice]):

Table[RandomSample[{a,b,c,d},1],{10^5}]//Flatten//Tally//Sort

RandomChoice[{a,b,c,d},10^5]//Tally//Sort

Table[RandomSample[10^Range[4]->{a,b,c,d},1],{10^5}]//Flatten//Tally//Sort

RandomChoice[10^Range[4]->{a,b,c,d},10^5]//Tally//Sort

OpenersExampleSection5(Spacer1)Possible Issues  (1)

If the sample size is larger than the population size, an error message is returned:

RandomSample[{a,b,c,d},5]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[RandomChoice, paclet:ref/RandomChoice])  ▪  (RefLink[RandomInteger, paclet:ref/RandomInteger])  ▪  (RefLink[SeedRandom, paclet:ref/SeedRandom])  ▪  (RefLink[BlockRandom, paclet:ref/BlockRandom])  ▪  (RefLink[Permutations, paclet:ref/Permutations])  ▪  (RefLink[Subsets, paclet:ref/Subsets])  ▪  (RefLink[Span, paclet:ref/Span])  ▪  (RefLink[Downsample, paclet:ref/Downsample])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Random Number Generation, paclet:tutorial/RandomNumberGenerationOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Random Number Generation, paclet:guide/RandomNumberGeneration])}, {▪(RefLinkPlain[Permutations, paclet:guide/Permutations])}, {▪(RefLinkPlain[Discrete Mathematics, paclet:guide/DiscreteMathematics])}, {▪(RefLinkPlain[Rearranging & Restructuring Lists, paclet:guide/RearrangingAndRestructuringLists])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])(WebLink[: Rearranging Lists, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])}})}})