Giter Site home page Giter Site logo

chainrand / chainrand-js Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 15 KB

Javascript library for verifiable hybrid-chain RNG.

Home Page: https://chainrand.io

JavaScript 100.00%
chainlink chainlink-vrf chainlink-hackathon-2021 javascript nodejs random random-number-generators cryptographic-random-generator es5-javascript

chainrand-js's Introduction

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.

Requirements

A Javascript ES5 envrionment: NodeJS, most web browsers, even older ones.

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): 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.

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

chainrand-js's People

Contributors

vectorized avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.