Little utility library for random value generation.
Latest commit 1a18cec Apr 4, 2016 @heydenberk Merge pull request #40 from heydenberk/0.6.2
Bump version to 0.6.2 to include lodash fixes.
Failed to load latest commit information.
src Correct case for lodash modules. Feb 25, 2016
test Fix non-deterministic test for the boolean generator. Feb 13, 2016
.npmignore Make the build process work better for the browser. Sep 7, 2015
LICENSE add Mozilla Public License v2 Nov 3, 2012
package.json Bump version to 0.6.2 to include lodash fixes. Apr 4, 2016

Download the latest compiled version of stochator.js (0.6):


Stochator is a tiny library providing for creating a variety of random value generators.

To create a Stochator object, simply invoke the constructor and pass it an options object with a kind property. If not provided, kind is 'float'.

Valid kinds include float, integer, set, color, a-z and A-Z.

Floating-point decimals

New in 0.5: available as Stochator.randomFloat(min, max, prng=Math.random)

It's very easy generate a float between 0 and 1.

var generator = new Stochator({});; // 0.9854211050551385; // 0.8784450970124453; // 0.1592887439765036

This is not very exciting because it simply wraps the built-in Math.random method.

Floats from an interval

New in 0.5: available as Stochator.randomFloat(min, max, prng=Math.random)

Specifying a min and a max allows us to create random numbers in the interval (min, max), not inclusive.

var radianGenerator = new Stochator({
    min: 0,
    max: Math.PI * 2
});; // 3.7084574239999655; // 1.021138034566463; // 4.012664264853087

Floats from a normal distribution

New in 0.5: available as: Stochator.fromDistribution.normal(mean, stdev, min, max, prng=Math.random)

We can also generate random floats from a normal distribution. Min and max are optional, and when provided will result in truncation of all results outside of [min, max].

var testScores = new Stochator({
    mean: 75,
    stdev: 14,
    min: 0,
    max: 100
});; // 59.437160028200125; // 80.18612670399554; // 75.81242027226946


New in 0.5: available as Stochator.randomInteger(min, max, prng=Math.random)

For integers, the interval [min, max] is inclusive. Notice that the optional argument name allows us to alias next to a more descriptive method name.

var die = new Stochator({
    kind: "integer",
    min: 1,
    max: 6
}, "roll");
die.roll(); // 6
die.roll(); // 1
die.roll(); // 2

New in 0.6 Available as Stochator.randomByte(prng=Math.random)

The special case of generating byte integers has its own shorthand.

Stochator.randomByte() // 186;


New in 0.6 Available as Stochator.randomBoolean(prng=Math.random)

Although trivally composable from other generators, randomBoolean is provided for convenience.

var bools = new Stochator({kind: "boolean"});; // false; // true; // true

Multiple results

If the next method (or a method aliased to it) is passed an integer n, it will return an n-length array of results. Using the die instance from the previous example:

die.roll(1); // [5]
die.roll(2); // [5, 3]
die.roll(5); // [6, 3, 6, 6, 5]

From sets

New in 0.5: available as Stochator.randomSetMember(values, prng=Math.random)

We can generate random values from arbitary sets.

var dayGenerator = new Stochator({
    kind: "set",
    values: ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
});; // friday; // monday; // monday

From sets with weights

New in 0.5: available as Stochator.weightedRandomSetMember(values, weights, prng=Math.random)

What if we favor the weekend? Well, we can pass weights, an array of the same length as values consisting of probabilities out of 1 that correspond to values.

var biasedDayGenerator = new Stochator({
    kind: "set",
    values: ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"],
    weights: [0.1, 0.1, 0.1, 0.1, 0.1, 0.25, 0.25]
});; // thursday; // sunday; // saturday

From sets without replacement

New in 0.5: available as Stochator.randomSetMemberWithoutReplacement(set, prng=Math.random)

Passing a replacement property with a falsy value will result in each random value generation to be removed from the set.

var chores = new Stochator({
    kind: "set",
    values: ["floors", "windows", "dishes"],
    replacement: false
var myChore =; // "windows"
var yourChore =; // "floors"
var hisChore =; // "dishes"
var noOnesChore =; // undefined

From predefined sets

New in 0.5: available as Stochator.randomColor(prng=Math.random), Stochator.randomLowercaseCharacter(prng=Math.random) and Stochator.randomUppercaseCharacter(prng=Math.random)

At present, predefined sets include "a-z", "A-Z" and "color".

var colorGenerator = new Stochator({
    kind: "color"
});; // { red: 122, green: 200, blue: 121 }; // { red: 129, green: 89, blue: 192 }; // { red: 125, green: 211, blue: 152 }

Note that this example passes an integer argument n to next which causes the stochator to return an array with the result of n calls to the generator.

var characterGenerator = new Stochator({
    kind: "a-z"
});""); // "uktlbkgufzjiztatmqelawfez"""); // "wdhygotehcfmrkjyuuovztxla"""); // "mbjxkhflycpxgdrtyyyevasga"


New in 0.6 Also available as: Stochator.randomCharacterFromRange(range, prng=Math.random) Stochator.randomAsciiString(expression, ignoreCase, prng=Math.random) Stochator.randomUnicodeString(expression, ignoreCase, prng=Math.random) Stochator.randomAsciiCharacter(prng=Math.random) Stochator.randomLowercaseCharacter(prng=Math.random) Stochator.randomUnicodeCharacter(prng=Math.random) Stochator.randomUppercaseCharacter(prng=Math.random)

For more flexibility than the pre-defined sets allow, we can use a regular expression.

var hexGenerator = new Stochator({
    kind: "string",
    expression: "#[A-F0-9]{6}"
});; // "#57A7BB"

Passing the ignoreCase argument with a value of true will cause characters in the a-z and A-Z ranges to be randomized in case.

var coolGreetingGenerator = new Stochator({
    kind: "string",
    expression: "Hello, world",
    ignoreCase: true
});; // "hELLo, WorlD"

expression can also be a true regular expression, in which case the trailing i flag will be honored.

var coolGreetingGenerator = new Stochator({
    kind: "string",
    expression: /Hello, world/i,
    ignoreCase: true
});; // "HELlO, WorLD"

All features supported by JavaScript regular expressions should work.

var fruitGenerator = new Stochator({
    kind: "string",
    expression: "((Apples|Oranges|Pears)! ?){1,5}"
});; // "Oranges!Pears! Apples! "

By default, the resulting characters are limited to ASCII. If you want unicode, pass the property unicode with a value of true.

var unicodeGenerator = new Stochator({
    kind: "string",
    expression: ".{10}",
    unicode: true
});; // "貧㒐⮧Ꮜ�惜攤鎃๵켕"

To override the maximum length of an unbounded match (* or {n,}), provide maxWildcard as true.

var binaryGenerator = new Stochator({
    kind: "string",
    expression: "Look at all this binary: ((0|1){256})",
    maxWildcard: 256
});; // "Look at all this binary: 1001011001010011100010101000011100010110010100100100000000110011010000000100000111111010110010100000010101001001100110011001111001111000111101000111010101011100010101101100001011110011110001101100001000001100101111110100001110100100000010111101111011111101"

Special thanks to randexp.js for inspiring this feature and informing its approach!


The constructor accepts an optional final argument which is passed the output of the random value generator. Its return value becomes the return value of next or its alias. To generate random boolean values, we can do:

var booleanGenerator = new Stochator({
    kind: "integer",
    min: 0,
    max: 1
}, Boolean);; // false; // true; // true

We can map the previously mentioned radianGenerator to the cosine of its values.

var radianSineGenerator = new Stochator({
    min: 0,
    max: Math.PI * 2
}, Math.cos);; // -0.31173382958096524; // -0.6424354006937544; // 0.6475980728835664

Mutators remember their previous result and, at each generation, apply the results of a specified stochator to create a new result.

(This is functionally equivalent to a Markov chain.)

var drunkardsWalk = new Stochator({
    kind: "integer",
    min: -1,
    max: 1
}, function(a, b) { return a + b; });; // [-1, -2, -2, -1, -1, -1, 0, 1, 1, 2]; // [3, 3, 3, 2, 1, 0, -1, 0, 0, 0]; // [0, 1, 0, -1, 0, 0, 1, 2, 1, 1]

Let's model a bank account's balance. How much money might you have after 10 years if you start with $1000, add $1000 every year, and get interest at a random rate between 1% and 5%?

var addInterest = function(interestRate, principal) {
    return (principal + 1000) * interestRate;
var savingsAccountBalance = new Stochator({
    kind: "float",
    min: 1.01,
    max: 1.05
}, addInterest);

savingsAccountBalance.setValue(1000); // Sets the initital value;

Multiple generators

If the Stochator constructor is passed multiple configuration objects, next (or its alias) returns an array of each random generated value.

To generate a random point, we might do:

var x = { kind: 'integer', min: 0, max: 480 };
var y = { kind: 'integer', min: 0, max: 360 };
var mutator = function(values) {
    return {
        x: values[0],
        y: values[1]
var randomPoint = new Stochator(x, y, mutator);; // { x: 79, y: 65 }; // { x: 151, y: 283 }; // { x: 5, y: 253 }

Overriding the pseudo-random number generator

New in 0.4.

Add a prng function to the configuration object to set your own pseudo-random number generator.

var seedrandom = require('seedrandom');
new Stochator({ prng: seedrandom('STOCHATOR') }).next(); // Always 0.4045178783365678

Using a seed

New in 0.4.

Rather than requiring seedrandom and passing a PRNG with a seed (as above), you can simply provide a seed value.

new Stochator({ seed: 'STOCHATOR' }).next(); // Still always 0.4045178783365678



Stochator now supports a kind of string, which supports generation of pseudo-random strings that satisfy provided regular expressions.


The entire project has been rewritten to use ES6 features, with translation handled by babel and browserify. In addition, Stochator has been supplemented with static methods to facilitate single-use value generators.


prng can be provided on a config object to override Math.random as the pseudo-random number generator. seed can be provided to seed the PRNG with an initial value. If both are provided, the PRNG will be the function returned by calling config.prng(config.seed); if just seed is provided, the PRNG will default to seedrandom rather than Math.random.