UNPKG

1.88 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function pickRandom
4
5Random pick one or more values from a one dimensional array.
6Array elements are picked using a random function with uniform or weighted distribution.
7
8
9## Syntax
10
11```js
12math.pickRandom(array)
13math.pickRandom(array, number)
14math.pickRandom(array, weights)
15math.pickRandom(array, number, weights)
16math.pickRandom(array, weights, number)
17math.pickRandom(array, { weights, number, elementWise })
18```
19
20### Parameters
21
22Parameter | Type | Description
23--------- | ---- | -----------
24`array` | Array &#124; Matrix | A one dimensional array
25`number` | Int | An int or float
26`weights` | Array &#124; Matrix | An array of ints or floats
27
28### Returns
29
30Type | Description
31---- | -----------
32number &#124; 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
38math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
39math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
40math.pickRandom([3, 6, 12, 2], { number: 2 }) // returns an array of two of the values in the array
41math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
42math.pickRandom([3, 6, 12, 2], 2, [1, 3, 2, 1]) // returns an array of two of the values in the array with weighted distribution
43math.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
45math.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)