pickRandom.md (1928B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 36 37 Type | Description 38 ---- | ----------- 39 40 41 ## Examples 42 43 ```js 44 math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array 45 math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array 46 math.pickRandom([3, 6, 12, 2], { number: 2 }) // returns an array of two of the values in the array 47 math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution 48 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 49 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 50 51 math.pickRandom([{x: 1.0, y: 2.0}, {x: 1.1, y: 2.0}], { elementWise: false }) 52 // returns one of the items in the array 53 ``` 54 55 56 ## See also 57 58 [random](random.md), 59 [randomInt](randomInt.md)