Giter Site home page Giter Site logo

fast-random's Introduction

fast-random

npm version

Simple fast seedable pseudo-random number generator

Purpose

Returns a single factory function that creates a new pseudo-random number generator (PRNG) based on the provided seed value. The generator returns 32-bit pseudo-random integers using the Lehmer/Park-Miller generator. This is a circular generator, which will cycle through all 2147483646 possible values before repeating

While not the most robust of PRNGs, the code has a very small footprint, is extremely quick and produces results good enough for many purposes. It has been written to work both within Node.js and in the browser

Installation

npm install --save fast-random

API Summary

Get a new random number generator

const random = require('fast-random');

const seed = 12345;
const generator = random(seed);

The returned generator contains the following functions

  • seed(SEED_VALUE): set the current seed value (SEED_VALUE is an integer)
  • nextInt(): get the next integer value (1 <= x <= 2147483646)
  • nextFloat(): get the next float value (0 <= x < 1)

Examples

General use

const random = require('fast-random');

// create a new generator
const seed = 12345;
const r = random(seed);

// produce some integer values
for (let i = 0; i < 8; ++i) {
	console.log(r.nextInt());
}

// reset the seed
r.seed(seed);

// produce some floating point values
for (let j = 0; j < 8; ++j) {
	console.log(r.nextFloat());
}

Set seed from current time

const random = require('fast-random');

const seed = Date.now();
const r = random(seed);

Get integers in a given range

const random = require('fast-random');

const seed = 12345;
const r = random(seed);

// get random number between a and b inclusive, ie (a <= x <= b)
function getRandomBetween(a, b) {
	return a + r.nextInt() % (b - a + 1);
}

More information

fast-random's People

Contributors

borilla avatar

Stargazers

Oscar Aguinagalde avatar Egill avatar  avatar Arnaud Juracek avatar Zach Compton avatar Boris Berak avatar Yehuda Katz avatar Hugo Piquemal avatar Raphaël Améaume avatar Florian Morel avatar Jeppe Burchardt avatar

Watchers

James Cloos avatar  avatar

fast-random's Issues

fast random as class

Thanks for the code. I made small changed, I hope it will be helpful:

var RandomGenerator = function(seed) {
  this.seed = function(seed) {
    if (seed === undefined)
      seed = (new Date()).getTime();
    if ((seed_value = seed % 2147483647) <= 0)
      seed_value += 2147483646;
  }
    
  this.random_int = function() {
    return seed_value = seed_value * 48271 % 2147483647;
  }
  
  this.random_float = function() {
    return (this.random_int() - 1) / 2147483646;
  }
  
  this.random_int_in_range = function(min, max) {
    return min + this.random_int() % (max - min + 1);  
  }
  
  this.seed(seed);
}
    
var generator = new RandomGenerator(12345);
for (var i=1; i<10; i++)
  console.log(generator.random_int() % 4);
  
generator.seed(12345);
for (var i=1; i<10; i++)
  console.log(generator.random_int_in_range(0, 3));

generator.seed();
for (var i=1; i<10; i++)
  console.log(generator.random_float());

Thanks

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.