1 |
|
2 |
|
3 | # Function pickRandom
|
4 |
|
5 | Random pick one or more values from a one dimensional array.
|
6 | Array elements are picked using a random function with uniform or weighted distribution.
|
7 |
|
8 |
|
9 | ## Syntax
|
10 |
|
11 | ```js
|
12 | math.pickRandom(array)
|
13 | math.pickRandom(array, number)
|
14 | math.pickRandom(array, weights)
|
15 | math.pickRandom(array, number, weights)
|
16 | math.pickRandom(array, weights, number)
|
17 | ```
|
18 |
|
19 | ### Parameters
|
20 |
|
21 | Parameter | Type | Description
|
22 | --------- | ---- | -----------
|
23 | `array` | Array | Matrix | A one dimensional array
|
24 | `number` | Int | An int or float
|
25 | `weights` | Array | Matrix | An array of ints or floats
|
26 |
|
27 | ### Returns
|
28 |
|
29 | Type | Description
|
30 | ---- | -----------
|
31 | number | Array | Returns a single random value from array when number is 1 or undefined. Returns an array with the configured number of elements when number is > 1.
|
32 |
|
33 |
|
34 | ## Examples
|
35 |
|
36 | ```js
|
37 | math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
|
38 | math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
|
39 | math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
|
40 | math.pickRandom([3, 6, 12, 2], 2, [1, 3, 2, 1]) // returns an array of two of the values in the array with weighted distribution
|
41 | math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1], 2) // returns an array of two of the values in the array with weighted distribution
|
42 | ```
|
43 |
|
44 |
|
45 | ## See also
|
46 |
|
47 | [random](random.md),
|
48 | [randomInt](randomInt.md)
|