UNPKG

deep-random-pick

Version:

Picks a pseudo-random item from a pool in several ways, including a recursive method that allows for more control over probability.

27 lines (14 loc) 1.1 kB
# Deep-Random Pick - recursive random picker A simple Javascript library made to pick random items from a pool through multiple (optional) layers. # Features Deep-Random-Pick allows for numerous ways to pick from a pool, with control over probability. ## Deep Random Pick Deep-Random-Pick's name comes from the `deepRandomPick` function (or just `randomPick` for brevity). This function picks from its arguments recursively, offering much more control over probability than `shallowPick`. However, the only time when `randomPick` can return an array is when the last item picked is an *empty* array. import * as randomPick from 'deep-random-pick'; let foo = ['bar', ['sploop']]; randomPick(foo); // Will output either 'bar' or 'sploop', but not ['sploop'] randomPick(foo, [69, 420], []); // Can output 'bar','sploop', 69, 420, or [] To better exemplify how the deep random pick works, here's a rather ugly function that will output the likelihood of its output. console.log(`${randomPick(.5,[.25,[.125,[.0625, .0625]]]) * 100}% chance`);