---
title: "RandomSample"
language: "en"
type: "Symbol"
summary: "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."
keywords: 
- simple random sample
- SRS
- sampling without replacement
- SRSWOR
- random permutation
- pseudorandom permutation
- pseudo-random permutation
- pseudorandom sample
- random subset
- shuffle
- shuffling
- urn problem
- weighted permutation
- randperm
- randperm
canonical_url: "https://reference.wolfram.com/language/ref/RandomSample.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Random Number Generation"
    link: "https://reference.wolfram.com/language/guide/RandomNumberGeneration.en.md"
  - 
    title: "Permutations"
    link: "https://reference.wolfram.com/language/guide/Permutations.en.md"
  - 
    title: "Discrete Mathematics"
    link: "https://reference.wolfram.com/language/guide/DiscreteMathematics.en.md"
  - 
    title: "Rearranging & Restructuring Lists"
    link: "https://reference.wolfram.com/language/guide/RearrangingAndRestructuringLists.en.md"
  - 
    title: "Tabular Transformation"
    link: "https://reference.wolfram.com/language/guide/TabularTransformation.en.md"
related_functions: 
  - 
    title: "RandomChoice"
    link: "https://reference.wolfram.com/language/ref/RandomChoice.en.md"
  - 
    title: "RandomInteger"
    link: "https://reference.wolfram.com/language/ref/RandomInteger.en.md"
  - 
    title: "SeedRandom"
    link: "https://reference.wolfram.com/language/ref/SeedRandom.en.md"
  - 
    title: "BlockRandom"
    link: "https://reference.wolfram.com/language/ref/BlockRandom.en.md"
  - 
    title: "Permutations"
    link: "https://reference.wolfram.com/language/ref/Permutations.en.md"
  - 
    title: "Subsets"
    link: "https://reference.wolfram.com/language/ref/Subsets.en.md"
  - 
    title: "Span"
    link: "https://reference.wolfram.com/language/ref/Span.en.md"
  - 
    title: "Downsample"
    link: "https://reference.wolfram.com/language/ref/Downsample.en.md"
related_tutorials: 
  - 
    title: "Random Number Generation"
    link: "https://reference.wolfram.com/language/tutorial/RandomNumberGenerationOverview.en.md"
---
# 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 ``ei`` more than once.

* ``RandomSample[{e1, e2, …}, n]`` samples each of the ``ei`` 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 (12)

### Basic Examples (3)

Find a sample in which no elements ever occur more than once:

```wl
In[1]:= RandomSample[Range[30], 20]

Out[1]= {22, 12, 24, 6, 21, 8, 25, 23, 29, 1, 2, 4, 30, 27, 14, 13, 19, 26, 10, 9}
```

---

Generate a random permutation:

```wl
In[1]:= RandomSample[Range[10]]

Out[1]= {3, 1, 5, 6, 8, 9, 4, 2, 10, 7}
```

---

Generate a random sample of 6 elements, or as many as there are if fewer:

```wl
In[1]:= RandomSample[Range[5], UpTo[6]]

Out[1]= {4, 5, 3, 2, 1}
```

### Scope (4)

The elements can be any expressions:

```wl
In[1]:= RandomSample[{Exp[x], x ^ 2, 1 / x, 2}]

Out[1]= {(1/x), 2, E^x, x^2}
```

---

With larger weights for later elements, later elements tend to be selected for the sample:

```wl
In[1]:= RandomSample[Range[30] -> Range[30], 10]

Out[1]= {24, 18, 21, 12, 25, 27, 15, 23, 30, 29}
```

---

Sample over all numbers between -10 and 10:

```wl
In[1]:= RandomSample[-10 ;; 10, 10]

Out[1]= {-4, -2, -7, -8, 0, -9, 1, 9, 10, -5}
```

Sample over even numbers between -10 and 10:

```wl
In[2]:= RandomSample[-10 ;; 10 ;; 2, 10]

Out[2]= {-8, 2, 0, 4, 8, 6, -10, 10, -6, -4}
```

---

Guarantee that a set of random integers over a big range has no repetitions:

```wl
In[1]:= RandomSample[1 ;; 2 ^ 30, 10]

Out[1]= {895596716, 873363267, 155594206, 1067841266, 7643595, 1039153695, 464854556, 742132254, 61477147, 429443009}
```

### Applications (1)

Successively fill in black squares down the page:

```wl
In[1]:= ArrayPlot[FoldList[ReplacePart[#, #2 -> 1]&, Table[0, {30}], RandomSample[Range[30], 30]]]

Out[1]= [image]
```

### Properties & Relations (3)

Use ``SeedRandom`` to get repeatable random values:

```wl
In[1]:= {RandomSample[Range[10]], RandomSample[Range[10]]}

Out[1]= {{10, 1, 3, 9, 6, 7, 5, 2, 4, 8}, {6, 2, 8, 9, 5, 3, 10, 4, 7, 1}}

In[2]:= {SeedRandom[1234];RandomSample[Range[10]], SeedRandom[1234];RandomSample[Range[10]]}

Out[2]= {{1, 7, 5, 6, 9, 3, 10, 4, 8, 2}, {1, 7, 5, 6, 9, 3, 10, 4, 8, 2}}
```

---

Use ``BlockRandom`` to block one use of ``RandomSample`` from affecting others:

```wl
In[1]:= {BlockRandom[RandomSample[Range[10]]], RandomSample[Range[10]]}

Out[1]= {{1, 2, 10, 3, 9, 6, 8, 4, 5, 7}, {1, 2, 10, 3, 9, 6, 8, 4, 5, 7}}
```

---

``RandomSample`` of size 1 follows the same distribution as ``RandomChoice`` :

```wl
In[1]:= Table[RandomSample[{a, b, c, d}, 1], {10 ^ 5}]//Flatten//Tally//Sort

Out[1]= {{a, 25255}, {b, 24661}, {c, 25167}, {d, 24917}}

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

Out[2]= {{a, 24978}, {b, 25145}, {c, 25070}, {d, 24807}}

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

Out[3]= {{a, 84}, {b, 933}, {c, 8890}, {d, 90093}}

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

Out[4]= {{a, 90}, {b, 903}, {c, 9126}, {d, 89881}}
```

### Possible Issues (1)

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

```wl
In[1]:= RandomSample[{a, b, c, d}, 5]
```

RandomSample::smplen: RandomSample cannot generate a sample of length 5, which is greater than the length of the sample set {a,b,c,d}.  If you want a choice of possibly repeated elements from the set, use RandomChoice.

```wl
Out[1]= RandomSample[{a, b, c, d}, 5]
```

## See Also

* [`RandomChoice`](https://reference.wolfram.com/language/ref/RandomChoice.en.md)
* [`RandomInteger`](https://reference.wolfram.com/language/ref/RandomInteger.en.md)
* [`SeedRandom`](https://reference.wolfram.com/language/ref/SeedRandom.en.md)
* [`BlockRandom`](https://reference.wolfram.com/language/ref/BlockRandom.en.md)
* [`Permutations`](https://reference.wolfram.com/language/ref/Permutations.en.md)
* [`Subsets`](https://reference.wolfram.com/language/ref/Subsets.en.md)
* [`Span`](https://reference.wolfram.com/language/ref/Span.en.md)
* [`Downsample`](https://reference.wolfram.com/language/ref/Downsample.en.md)

## Tech Notes

* [Random Number Generation](https://reference.wolfram.com/language/tutorial/RandomNumberGenerationOverview.en.md)

## Related Guides

* [Random Number Generation](https://reference.wolfram.com/language/guide/RandomNumberGeneration.en.md)
* [`Permutations`](https://reference.wolfram.com/language/guide/Permutations.en.md)
* [Discrete Mathematics](https://reference.wolfram.com/language/guide/DiscreteMathematics.en.md)
* [Rearranging & Restructuring Lists](https://reference.wolfram.com/language/guide/RearrangingAndRestructuringLists.en.md)
* [Tabular Transformation](https://reference.wolfram.com/language/guide/TabularTransformation.en.md)

## Related Links

* [An Elementary Introduction to the Wolfram Language: Rearranging Lists](https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html)

## History

* [Introduced in 2007 (6.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn60.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2015 (10.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn103.en.md)