Random

Javascript's go-to source of randomness is Math.random() which returns a random number equal or above 0 and less than 1. This is perfect for uniform distribution on a percentage scale.

We sometimes want the values on a different scale. Ixfx has a few helper functions for this common need:

// repl-pad
import { integer, float } from 'https://unpkg.com/ixfx/dist/random.js';
integer(10); // Random integer value 0..<10
float(100); // Random floating point number 0..<100 

Weighted distributions

Another issue with Math.random is it's roughly even distribution. You can see this below, the horizontal axis shows values from 0 on the left to 1 on the right. Note how the plot mostly fills out evenly along the horizontal axis.

To make some numbers more likely than others, you may want some form of weighted distribution.

ixfx's Random.weighted uses an easing function to shape random numbers.

When using the quadIn easing (the default), note how the density of random values skews toward 0, visually shown as the left-most part of the bar.

// repl-pad
import { weighted } from 'https://unpkg.com/ixfx/dist/random.js';
// Yields 0-1 (inclusive) random number
weighted(`quadIn`);
weighted(`quadOut`);

Random integers (ie. whole numbers) can be produced with Random.weightedInteger. This is useful for producing random array indexes.

A range is provided to the function, with the return value always below the maximum (ie, it is exclusive). The minimum, 0 by default, might be returned (ie, it is inclusive).

// repl-pad
import { weightedInteger } from 'https://unpkg.com/ixfx/dist/random.js';
weightedInteger(10);      // 0-9
weightedInteger(10, 20);  // 10-19
weightedInteger(100, `quadIn`);       // 0-99, specifying the easing function
weightedInteger(100, 200, `quadOut`); // 100-199, specifying the easing function

To use for accessing an array randomly:

const list = [`mango`, `kiwi`, `grape`];
// Yields random item from list
list[weightedInteger(list.length)];

Gaussian

Gaussian distribution has a 'bell curve' shape, centred around the middle. In other words, you'd expect to get more random values around 0.5 than 0 or 1. ixfx's Random.gaussian provides this.

// repl-pad#2
import { gaussian } from 'https://unpkg.com/ixfx/dist/random.js';

// Yields a random number between 0..1
gaussian();

The function takes a skew parameter which shifts the centre of the curve.

// repl-pad#2
// Shifts distribution to right, closer to 1
gaussian(0.1);
// Shifts distribution to the left, closer to 0
gaussian(6);

More random

// repl-pad
import { minutesMs, secondsMs } from 'https://unpkg.com/ixfx/dist/random.js';
// minutesMs and secondsMs compute random millisecond values
minutesMs(5); // Random timeout of up to 5 minutes
secondsMs(5); // Random timeout of up to 5 seconds

With arrays

// repl-pad
import { arrayElement, arrayIndex, integerUniqueGen } from 'https://unpkg.com/ixfx/dist/random.js';

const v = [`blue`, `red`, `orange`];

arrayElement(v); // Returns a random value
arrayIndex(v); // Random array index

// Produce unique set of random integers 0..<10
const values = [...integerUniqueGen(10)];

Other values:

// repl-pad
import { hue, string } from 'https://unpkg.com/ixfx/dist/random.js';

hue(); // Compute random hue value 0...359
string(10); // Random string 10 letters long