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

chainrand

v0.0.3

Published

Verifiable hybrid-chain random number generator.

Downloads

6

Readme

Chainrand-js — Verifiable hybrid-chain RNG.

Many applications require off-chain generation of random numbers for efficiency, security, etc.

This class allows you to generate a stream of deterministic, high-quality,
cryptographically secure random numbers.

By seeding it with a Chainlink VRF result that is requested only once for the project,
it can be used to demonstrate that the random numbers are not cherry-picked.

Installation

Browser / CDN:

<script src="https://cdn.jsdelivr.net/npm/chainrand/chainrand.min.js"></script>

NPM:

npm i chainrand

Or you can clone/download this GitHub repository.

Usage

var rng = chainrand.CRNG("base10(<RNG_VRF_RESULT>)" + "<RNG_SEED_KEY>")
// prints 10 determinstic random numbers between [0, 1)
for (var i = 0; i < 10; ++i) {
    console.log(rng())
}

Reproducibility

Current and future versions of this library will generate the same stream of random numbers from the same seed.

Functions

Constructor

chainrand.CRNG(seed)

Creates an instance of the crng initialized with the seed.

Parameters:

  • seed: String If empty, defaults to the empty string "".

Example:

var crng = chainrand.CRNG("base10(<RNG_VRF_RESULT>)" + "<RNG_SEED_KEY>")

random

crng.random(): Number

Alias for crng(). Returns a random number uniformly distributed in [0, 1).
The numbers are in multiples of 2**-53.

Parameters: none

Returns: A random number uniformly distributed in [0, 1).

randrange

crng.randrange(start, stop[, step]): Integer
crng.randrange(stop): Integer

Returns a random integer uniformly distributed in [start, stop).
The integers are spaced with intervals of |step|.

Parameters:

  • start: Integer The start of the range. (optional, default=0)
  • stop: Integer The end of the range.
  • step: Integer The interval step. (optional, default=1)

Returns:

A random integer uniformly distributed in [start, stop).

Examples:

r = crng.randrange(3) // returns a random number in {0,1,2}
r = crng.randrange(-3) // returns a random number in {0,-1,-2}
r = crng.randrange(0, 6, 2) // returns a random number in {0,2,4}
r = crng.randrange(5, 0, 1) // returns a random number in {5,4,3,2,1}
r = crng.randrange(5, -5, -2) // returns a random number in {5,3,1,-1,-3}

randint

crng.randint(start, stop[, step]): Integer
crng.randint(stop): Integer

Returns a random integer uniformly distributed in [start, stop].
The integers are spaced with intervals of |step|.

Parameters:

  • start: Integer The start of the range. (optional, default=0)
  • stop: Integer The end of the range.
  • step: Integer The interval step. (optional, default=1)

Returns:

A random integer uniformly distributed in [start, stop].

Examples:

r = crng.randint(3) // returns a random number in {0,1,2,3}
r = crng.randint(-3) // returns a random number in {0,-1,-2,-3}
r = crng.randint(-3, 1) // returns a random number in {-3,-2,-1,0,1}
r = crng.randint(3, -1) // returns a random number in {3,2,1,0,-1}

choice

crng.choice(population[, weights]): Array

Returns a random element from the population.

If weights is not provided, every element of population will be equally weighted.

If weights is a non-empty array and is of different length to population,
only the first Math.min(population.length, weights.length) elements of population are sampled.

If the sum of the weights is less than or equal to zero,
every element of population will be equally weighted.

Parameters:

  • population: Array The population.
  • weights: Array<Number> The weights of the population. (optional)

Returns:

A random element in the population.

Examples:

/* returns a random number in {1,2,3} */
r = crng.choice([1,2,3]) 

/* returns a random number in {1,2,3}
   with the weights {1:10, 2:1, 3:0.1} */
r = crng.choice([1,2,3], [10,1,0.1]) 

sample

crng.sample(population, k=1[, weights]): Array

Returns k random elements from the population, sampling without replacement.

If k is more than the length of the population, only k elements will be returned.

If weights is not provided, every element of population will be equally weighted.

If weights is a non-empty array and is of different length to population,
only the first Math.min(population.length, weights.length) elements of population are sampled.

If the sum of the weights is less than or equal to zero,
every element of population will be equally weighted.

Parameters:

  • population: Array The population.
  • k: Integer The number of elements to choose.
  • weights: Array<Number> The weights of the population. (optional)

Returns:

An array of k random elements from the population.

Examples:

/* returns an array of 1 random element from {1,2,3} */
r = crng.sample([1,2,3]) 

/* returns an array of 2 random elements from {1,2,3} */
r = crng.sample([1,2,3], 2) 

/* returns an array of 2 random elements from {1,2,3}
   with the weights {1:10, 2:1, 3:0.1} */
r = crng.sample([1,2,3], 2, [10,1,0.1]) 

shuffle

crng.shuffle(population)

Shuffles the array in-place.

Parameters:

  • population: Array The population.

Returns:

The shuffled array.

gauss

crng.gauss(mu=0.0, sigma=1.0): Number

Normal distribution, also called the Gaussian distribution.

Parameters:

  • mu: Number The mean. (optional, default=0.0)
  • sigma: Number The standard deviation. (optional, default=1.0)

Returns:

A random number from the Gaussian distribution.

License

MIT