函数 pickRandom #
从一维数组中随机选取一个或多个值。数组元素的选取使用具有均匀或加权分布的随机函数。
Syntax #
math.pickRandom(array)
math.pickRandom(array, number)
math.pickRandom(array, weights)
math.pickRandom(array, number, weights)
math.pickRandom(array, weights, number)
math.pickRandom(array, { weights, number, elementWise })
Parameters #
| Parameter | Type | Description |
|---|---|---|
array |
Array | Matrix | 一维数组 |
number |
整数 | 一个整数或浮点数 |
权重 |
Array | Matrix | 整数或浮点数数组 |
Returns #
| Type | Description |
|---|---|
| 数字 | 数组 | 当 number 未定义时,返回数组中的单个随机值。当 number 已定义时,返回具有配置数量元素的数组。 |
Throws #
Type | Description —- | ———–
Examples #
math.pickRandom([3, 6, 12, 2]) // returns one of the values in the array
math.pickRandom([3, 6, 12, 2], 2) // returns an array of two of the values in the array
math.pickRandom([3, 6, 12, 2], { number: 2 }) // returns an array of two of the values in the array
math.pickRandom([3, 6, 12, 2], [1, 3, 2, 1]) // returns one of the values in the array with weighted distribution
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
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
math.pickRandom([{x: 1.0, y: 2.0}, {x: 1.1, y: 2.0}], { elementWise: false })
// returns one of the items in the array