UNPKG

1.59 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)
17```
18
19### Parameters
20
21Parameter | Type | Description
22--------- | ---- | -----------
23`array` | Array &#124; Matrix | A one dimensional array
24`number` | Int | An int or float
25`weights` | Array &#124; Matrix | An array of ints or floats
26
27### Returns
28
29Type | Description
30---- | -----------
31number &#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.
32
33
34## Examples
35
36```js
37math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
38math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
39math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
40math.pickRandom([3, 6, 12, 2], 2, [1, 3, 2, 1]) // returns an array of two of the values in the array with weighted distribution
41math.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)