Skip to content

nspl\rnd

Ihor Burlachenko edited this page May 19, 2016 · 1 revision

Helps to pick random items from sequences of data


choice($sequence)

Returns a random item from a non-empty sequence

weightedChoice($weightPairs)

Returns a random item from a non-empty sequence of items with associated weights presented as pairs (item, weight)

use function \nspl\rnd\weightedChoice;
use function \nspl\a\pairs;

$nextPet = weightedChoice([['cat', 20], ['hamster', 30], ['dog', 50]]);
$nextFavouriteColor = weightedChoice(pairs(array(
    'red' => 0.2,
    'green' => 0.3,
    'blue' => 0.5,
)));
sample($population, $length, $preserveKeys = false)

Returns a k length list of unique items chosen from the population sequence

Check more \nspl\rnd examples here.

Clone this wiki locally