# stochasm

Create functions to generate random values.

# Stochasm

A JavaScript component to create functions that generate random values.

It can be very useful to generate random numbers that are governed by properties of different types of distributions. Such distributions are useful for modeling numerical behavior and response of systems.

This module was forked from https://github.com/heydenberk/stochasm that was created by Eric Heydenberk. Why did I fork it?

• Unmaintained
• Written in CoffeeScript
• Not a UMD
• No tests
• No support to modify the random number generation

Rather than pester Eric about changing any of the above, a fork seemed more reasonable.

`stochasm` is a portmanteau of stochastic and chasm.

``````npm install --save stochasm
``````
``````component install jprichardson/stochasm
``````
``````bower install stochasm
``````

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

Valid kinds include `float`, `integer`, `set`.

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

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

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

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].

For integers, the interval [min, max] is inclusive.

if `next()` feels out of place for your use case, just rename the method:

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:

We can generate random values from arbitary sets.

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`.

Note: This functionality may be removed.

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

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:

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

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

(This is functionally equivalent to a Markov chain.)

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%?

If the stochasm function 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:

Want to bring your own random number generator to the party? Whether you're working with Node.js and want to use `crypto.getRandomValues()` or in the browser and want to use `window.crypto.getRandomValues()`, you can. You could also return a constant to see how your system may respond to certain conditions in testing.

Note: At this time, it's assumed that any function that you set `rand` to will return a number in the range of `[0,1)`.

As stated above, this code was forked from the Node.js module `stochator` (https://github.com/heydenberk/stochasm) that was created by Eric Heydenberk. Eric Heydenberk deserves much of the credit for coming up with such an awesome idea.