mathjs/docs/reference/functions/pickRandom.md
2018-06-16 15:39:07 +02:00

53 lines
2.0 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-pickrandom">Function pickRandom <a href="#function-pickrandom" title="Permalink">#</a></h1>
Random pick one or more values from a one dimensional array.
Array elements are picked using a random function with uniform or weighted distribution.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.pickRandom(array)
math.pickRandom(array, number)
math.pickRandom(array, weights)
math.pickRandom(array, number, weights)
math.pickRandom(array, weights, number)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`array` | Array | A one dimensional array
`number` | Int | An int or float
`weights` | Array | An array of ints or floats
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
number &#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.
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```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
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[random](random.html),
[randomInt](randomInt.html)