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 | math.pickRandom(array, { weights, number, elementWise })
|
18 | ```
|
19 |
|
20 | ### Parameters
|
21 |
|
22 | Parameter | Type | Description
|
23 | --------- | ---- | -----------
|
24 | `array` | Array | Matrix | A one dimensional array
|
25 | `number` | Int | An int or float
|
26 | `weights` | Array | Matrix | An array of ints or floats
|
27 |
|
28 | ### Returns
|
29 |
|
30 | Type | Description
|
31 | ---- | -----------
|
32 | 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.
|
33 |
|
34 |
|
35 | ## Examples
|
36 |
|
37 | ```js
|
38 | math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
|
39 | math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
|
40 | math.pickRandom([3, 6, 12, 2], { number: 2 }) // returns an array of two of the values in the array
|
41 | math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
|
42 | 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
|
43 | 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
|
44 |
|
45 | math.pickRandom([{x: 1.0, y: 2.0}, {x: 1.1, y: 2.0}], { elementWise: false })
|
46 | // returns one of the items in the array
|
47 | ```
|
48 |
|
49 |
|
50 | ## See also
|
51 |
|
52 | [random](random.md),
|
53 | [randomInt](randomInt.md)
|