1.1.0 • Published 6 years ago

drand v1.1.0

Weekly downloads
7
License
MIT
Repository
-
Last release
6 years ago

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.

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago