UNPKG

1.82 kBJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed) {
4 const distribution = load(require('./distribution'))
5
6 /**
7 * Random pick one or more values from a one dimensional array.
8 * Array elements are picked using a random function with uniform or weighted distribution.
9 *
10 * Syntax:
11 *
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 * Examples:
19 *
20 * math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
21 * math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
22 * math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
23 * 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
24 * 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
25 *
26 * See also:
27 *
28 * random, randomInt
29 *
30 * @param {Array} array A one dimensional array
31 * @param {Int} number An int or float
32 * @param {Array} weights An array of ints or floats
33 * @return {number | Array} Returns a single random value from array when number is 1 or undefined.
34 * Returns an array with the configured number of elements when number is > 1.
35 */
36 // TODO: rework pickRandom to a typed-function
37 const pickRandom = distribution('uniform').pickRandom
38
39 pickRandom.toTex = undefined // use default template
40
41 return pickRandom
42}
43
44exports.name = 'pickRandom'
45exports.factory = factory