RandomSelect
RandomSelect
RandomSelect[ list ] randomly selects one or more elements from a list.
RandomSelect[ list ]
RandomSelect[ list ] returns one randomly chosen element of list. The elements can be any expressions. Each time the exercise is generated, a new choice is made, so the outputs below are only examples.
| Example | in | out |
|---|---|---|
| A random prime number. | RandomSelect[ {2, 3, 5, 7} ] |
for example 3 |
| A random expression. | RandomSelect[ {x+1, 2x, x^2} ] |
for example x+1 |
RandomSelect[ list, n ]
RandomSelect[ list, n ] returns a list of n randomly chosen elements. The same element may be chosen more than once; use the option AllowDuplicates to prevent this.
| Example | in | out |
|---|---|---|
3 elements, possibly the same. |
RandomSelect[ {a, b, c, d}, 3 ] |
for example {d, a, a} |
With n = 1, the result is a list with one element. |
RandomSelect[ {a, b, c}, 1 ] |
for example {a} |
AllowDuplicates
If set to False, every element is chosen at most once. n must then be at most the length of the list. Default: True.
| Syntax | Example | out |
|---|---|---|
RandomSelect[ list, n, AllowDuplicates=>False ] |
RandomSelect[ {2, 4, 6}, 2, AllowDuplicates=>False ] |
for example {2, 6} |
Exclude
A list of elements that must not be chosen.
| Syntax | Example | out |
|---|---|---|
RandomSelect[ list, n, Exclude=>{e1, e2, ...} ] |
RandomSelect[ {a, b, c}, 3, Exclude=>{a} ] |
for example {b, b, c} |
Weights
A list of non-negative integers, one for each element, that sets how likely each element is to be chosen. An element with weight 3 is three times as likely as an element with weight 1; an element with weight 0 is never chosen. Default: all weights equal.
| Syntax | Example | out |
|---|---|---|
RandomSelect[ list, n, Weights=>{w1, w2, ...} ] |
RandomSelect[ {a, b, c}, 6, Weights=>{3, 1, 0} ] |
for example {a, a, b, a, a, a} |
- Zero elements:
RandomSelect[ {a, b, c}, 0 ]returns{}. - Sets can be used instead of lists:
RandomSelect[ Set[1, 2, 3], 2 ]. - Weights and Exclude together: the weights belong to the elements that remain after
Exclude.RandomSelect[ {a, b, c}, Exclude=>{a}, Weights=>{1, 5} ]is correct; giving three weights here is an error. - Older notation:
RandomSelect[ a, b, c ], without the list, still works but should not be used in new scripts.
| Invalid input | Result |
|---|---|
RandomSelect[ {a, b, c}, Weights=>{1, 1} ] |
Error: The given weights do not match the given elements |
RandomSelect[ {a, b, c}, Foo=>1 ] |
Error: Invalid associative argument for RandomSelect: Foo |
- With
AllowDuplicates=>False,nmust not be larger than the number of elements.RandomSelect[ {2, 4, 6}, 4, AllowDuplicates=>False ]stops the script with an internal error. The same happens for an empty list, or whenExcluderemoves all elements. Excludemust be a list.Exclude=>a(without curly braces) is ignored without an error, soacan still be chosen. WriteExclude=>{a}.Weightsmust be integers. Other weights, such as{0.5, 1, 1}, are ignored without an error and all elements get the same weight.
See also: RandomInt, RandomSample, Shuffle, RandVars