Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including drand with all npm packages installed. Try it out:

const Drand = require('drand') const _ = require('lodash') const seed = 12346 /* * ============================================================================= * You can create an instance of Drand to pass around. */ const r = new Drand(seed) const demo = { float: r.rand(), // [0, 1) floatUpper: r.rand(33.33333), // [0, upper) floatBound: r.rand(Math.E, Math.PI), // [lower, upper) int: r.randInt(), // [-2^32, 2^32-1) intUpper: r.randInt(128), // [0, upper) intBound: r.randInt(256, 512), long: r.randLong(), // [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER) longUpper: r.randLong(Math.pow(2, 42)), // [0, upper) longBound: r.randLong(Math.pow(2, 35), Math.pow(2, 40)), } console.log(demo) /* * ============================================================================= * It maybe easier to just use the static functions attached to `Math`. These * functions are attached by default when the library is loaded, but you can * optionally seed the static instance. Note that you can seed the static * instance as many times as needed. */ // Optional Drand.setGlobal(seed) const globalDemo = { float: Math.drand(), // [0, 1) floatUpper: Math.drand(33.33333), // [0, upper) floatBound: Math.drand(Math.E, Math.PI), // [lower, upper) int: Math.drandInt(), // [-2^32, 2^32-1) intUpper: Math.drandInt(128), // [0, upper) intBound: Math.drandInt(256, 512), long: Math.drandLong(), // [NumbeMath.dMIN_SAFE_INTEGER, NumbeMath.dMAX_SAFE_INTEGER) longUpper: Math.drandLong(Math.pow(2, 42)), // [0, upper) longBound: Math.drandLong(Math.pow(2, 35), Math.pow(2, 40)), } console.log(globalDemo) console.log(`_.isEqual(demo, globalDemo) = ${_.isEqual(demo, globalDemo)}`)

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

drand v1.1.0

Generates random number for Nodejs (and browser) using Mersenne Twister.

Drand

Generates random number for Nodejs (and browser) using Mersenne Twister.

NPM package

Build Status Coverage Status dependencies Status

Usage

Try Drand on RunKit

Real Numbers

FunctionDescriptionBounds
r.rand()Returns a continuous random number between 0 and 1[0, 1)
r.rand(ub)Returns a continuous random number bounded by 0 and the provided upperbound[0, upperBound)
r.rand(lb, ub)Returns a continuous random number bounded by the parameters[lowerBound, upperBound)

Integers - 32 bit (31 + 1 sign bit)

The following functions use a 32 bit sign safe 32 bit mask.

FunctionDescriptionBounds
r.randInt()Returns a random integer between 0 and 1[-2^32, 2^32-1)
r.randInt(ub)Returns a random integer bounded by 0 and the provided upperbound[0, upperBound)
r.randInt(lb, ub)Returns a random integer bounded by the parameters[lowerBound, upperBound)

Longs - 54 bit (53 + 1 sign bit)

FunctionDescriptionBounds
r.randLong()Returns a random integer between 0 and 1[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
r.randLong(ub)Returns a random integer bounded by 0 and the provided upperbound[0, upperBound)
r.randLong(lb, ub)Returns a random integer bounded by the parameters[lowerBound, upperBound)

Global -> Math

All of the functions described above are available from the global Math context. The functions are added by default when loading the library. Note that the static instance is seeded with a random key. You can access them using

Math.drand()
Math.drandInt()
Math.drandLong()

You can also seed the static instance using Drand.setGlobal(seed). This can be done at any time and as many times as desired. For example

const seed = 1234
Drand.setGlobal(seed)
const rand1 = Math.drand()
Drand.setGlobal(seed)
const rand2 = Math.drand()

assert(rand1 === rand2)

See the demo on Runkit for more.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free