mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
49 lines
1.6 KiB
Markdown
49 lines
1.6 KiB
Markdown
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
# Function pickRandom
|
|
|
|
Random pick one or more values from a one dimensional array.
|
|
Array elements are picked using a random function with uniform or weighted distribution.
|
|
|
|
|
|
## Syntax
|
|
|
|
```js
|
|
math.pickRandom(array)
|
|
math.pickRandom(array, number)
|
|
math.pickRandom(array, weights)
|
|
math.pickRandom(array, number, weights)
|
|
math.pickRandom(array, weights, number)
|
|
```
|
|
|
|
### Parameters
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`array` | Array | Matrix | A one dimensional array
|
|
`number` | Int | An int or float
|
|
`weights` | Array | Matrix | An array of ints or floats
|
|
|
|
### Returns
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
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.
|
|
|
|
|
|
## Examples
|
|
|
|
```js
|
|
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], [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
|
|
```
|
|
|
|
|
|
## See also
|
|
|
|
[random](random.md),
|
|
[randomInt](randomInt.md)
|