drand
Generates random number for Nodejs (and browser) using Mersenne Twister.
Last updated 3 years ago by de44 .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install drand 
SYNC missed versions from official npm registry.

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

Function Description Bounds
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.

Function Description Bounds
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)

Function Description Bounds
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.

Current Tags

  • 1.1.0                                ...           latest (3 years ago)

9 Versions

  • 1.1.0                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (9)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |