pseudo-math-random

    1.0.0 • Public • Published

    pseudo-math-random

    Same algorithm as Math.random but with a custom seed.
    Useful for reproducible tests and benchmarks. Same API as math-random-seed which uses crypto (secure) while this uses xorshift128+ (60x faster).

    npm status node Travis build status JavaScript Style Guide

    Usage

    const pmr = require('pseudo-math-random')
    const random = pmr('a seed')
     
    console.log(random()) // Always 0.32911525012180043
    console.log(random()) // Always 0.5563213847156248

    API

    random = pmr([seed])

    Create a new random number generator. The seed argument must be a string or Buffer. It is hashed once to counter short or zero-filled seeds. If no seed is provided one will be generated.

    num = random()

    Get a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). Does not provide cryptographically secure random numbers.

    Install

    With npm do:

    npm install pseudo-math-random
    

    Benchmark

    $ node benchmark.js
    node v10.14.1
    pseudo-math-random x 23,050,253 ops/sec ±0.24% (94 runs sampled)
    math-random-seed x 377,674 ops/sec ±1.50% (86 runs sampled)
    Fastest is pseudo-math-random
    

    NB. Speed isn't everything. Decide for yourself which properties you need.

    License

    MIT © 2019-present Vincent Weevers

    Install

    npm i pseudo-math-random

    DownloadsWeekly Downloads

    9

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    4.58 kB

    Total Files

    4

    Last publish

    Collaborators

    • vweevers