npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rndgen

v1.0.1

Published

Class generating deterministic or non-deterministic random data

Downloads

4

Readme

In a Nutshell

A class library returning random data, random integers, and random floating point number either using crypto as source, or deterministically deriving a sequence from sha512 hash function using the caller submitted key.

Reference

const RndGen = require('rndgen');

var rg = new RndGen(); // Non-deterministic instance
rg.getBytes(10);       // 10 bytes of random data in Buffer
rg.getInt();           // random integer 0..9007199254740991 inclusively
rg.getInt(42);         // random integer 0..42 inclusively
rg.getInt(100, 200);   // random integer 100..200 inclusively
rg.rand()              // random number 0.0 <= x < 1.0

var rg = new RndGen('secret key!'); // Deterministic instance
// Same interfaces are available.  Same key and same call order
// produces same sequence of return values.

RndGen()

Constructor for undeterministic random class. All functions called using an instance created like this, return non-reproducible data.

RndGen(key)

Constructor for deterministic random class. All call chains to class functions using an instance created like this produce a deterministic return data that can be reproduced using the same key and same call chain.

RndGen.prototype.getBytes(bytes)

Return a Buffer of random bytes. Number of bytes passed as an argument.

RndGen.prototype.getInt(min, max)

Return a random integer in range min..max inclusively. The limits must be nonnegative safe integers and min must not be bigger than max. If one argument is passed, it is considered the maximum and the minimum defaults to zero. If no arguments are passed, the minimum defaults to zero and the maximum defaults to Number.MAX_SAFE_INTEGER.

RndGen.prototype.rand()

Return a random number x greater or equal to 0 but less than 1.

Because of javascript number type, rand() function can return 2^53 different values and of course they can be enumerated. You'd be very lucky to see any of these particular ones, but rest assured, it is possible that any of those pop up. One that absolutely does NOT pop out is 1.00000000000000000000.

// 1                0.00000000000000000000
// 2                0.00000000000000011102
// 3                0.00000000000000022204
// 4                0.00000000000000033307
// 5                0.00000000000000044409
// 6                0.00000000000000055511
// 7                0.00000000000000066613
// 8                0.00000000000000077716
// 9                0.00000000000000088818
// 10               0.00000000000000099920
//                  .
//                  .
//                  .
// 9007199254740982 0.99999999999999877875
// 9007199254740983 0.99999999999999888978
// 9007199254740984 0.99999999999999900080
// 9007199254740985 0.99999999999999911182
// 9007199254740986 0.99999999999999922284
// 9007199254740987 0.99999999999999933387
// 9007199254740988 0.99999999999999944489
// 9007199254740989 0.99999999999999955591
// 9007199254740990 0.99999999999999966693
// 9007199254740991 0.99999999999999977796
// 9007199254740992 0.99999999999999988898

RndGen.prototype.shuffle(a)

Shuffles array or buffer into random order. The ordering is done inplace and the return value is the given array again.

const RndGen = require('rndgen');
var rg = new RndGen();

// Produce array of integers 0..99 in random order
var seq =  rg.shuffle(new Array(100).fill().map(function(x,i){return i;}));

BigNum

I don't want to introduce node-bignum package as a dependency in this fairly simple and small class since it would bloat it e.g. in case of serverless environments. However in case you are wondering, and why wouldn't you be, how this library could be used for generating evenly distributed bignums, this is how it is done:

"use strict";

const RndGen = require('./rndgen.js');
const BigNum = require('bignum');

var rg = new RndGen();

function rndBigNum(n) {
    if (! BigNum.isBigNum(n)) {
        n = BigNum(n);
    }
    if (! (BigNum.isBigNum(n) && n.ge(0))) {
        throw new Error('Bad limit');
    }
    if (n.eq(0)) {
        return BigNum(0);
    }
    var d, r, b = n.bitLength();
    do {
        d = rg.getBytes(Math.ceil(b / 8));
        if (b % 8) {
            d[0] >>= (8 - (b % 8))
        }
        r = BigNum.fromBuffer(d)
    } while (r.gt(n));
    return r;
}

module.exports = rndBigNum;

Naturally this would be more useful for scenarios where a deterministic sequence is required, in which case you'd of course be using a key for your RndGen instance. The principle is the same for other big integer packages like node-bigint.

Author

Timo J. Rinne [email protected]

License

GPL-2.0