Skip to content

Instantly share code, notes, and snippets.

@ooflorent
Last active April 18, 2018 03:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ooflorent/f712486879d7faaa9eb5 to your computer and use it in GitHub Desktop.
Save ooflorent/f712486879d7faaa9eb5 to your computer and use it in GitHub Desktop.
Seeded random number generator

rng

Seeded random number generator.
Down to ~500 bytes when minified.
Down to ~260 bytes when gzipped.

Usage

var rng = require('./rng');
var rand = rng(1337);

rand.int(100);                    // 57
rand.float();                     // 0.48787342294965175
rand.bool();                      // false
rand.range(4, 12);                // 10
rand.pick(['foo', 'bar', 'baz']); // 'baz'

API

rng(seed)

Returns a RNG using the specified seed.

rand.int(max)

Returns an integer within [0, max).

rand.float()

Returns a float within [0.0, 1.0).

rand.bool()

Returns a boolean.

rand.range(min, max)

Returns an integer within [min, max).

rand.pick(source)

Returns an element from the source.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Florent Cailhol <https://github.com/ooflorent>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
module.exports = function(seed) {
var random = whrandom(seed);
var rng = {
/**
* Return an integer within [0, max).
*
* @param {int} [max]
* @return {int}
* @api public
*/
int: function(max) {
return random() * (max || 0xfffffff) | 0;
},
/**
* Return a float within [0.0, 1.0).
*
* @return {float}
* @api public
*/
float: function() {
return random();
},
/**
* Return a boolean.
*
* @return {Boolean}
* @api public
*/
bool: function() {
return random() > 0.5;
},
/**
* Return an integer within [min, max).
*
* @param {int} min
* @param {int} max
* @return {int}
* @api public
*/
range: function(min, max) {
return rng.int(max - min) + min;
},
/**
* Pick an element from the source.
*
* @param {mixed[]} source
* @return {mixed}
* @api public
*/
pick: function(source) {
return source[rng.range(0, source.length)];
}
};
return rng;
};
/**
* Generate a seeded random number using Python's whrandom implementation.
* See https://github.com/ianb/whrandom for more information.
*
* @param {int} [seed]
* @return {Function}
* @api private
*/
function whrandom(seed) {
if (!seed) {
seed = Date.now();
}
var x = (seed % 30268) + 1;
seed = (seed - (seed % 30268)) / 30268;
var y = (seed % 30306) + 1;
seed = (seed - (seed % 30306)) / 30306;
var z = (seed % 30322) + 1;
seed = (seed - (seed % 30322)) / 30322;
return function() {
x = (171 * x) % 30269;
y = (172 * y) % 30307;
z = (170 * z) % 30323;
return (x / 30269.0 + y / 30307.0 + z / 30323.0) % 1.0;
};
}
@sasi1346862
Copy link

Hiiii.....I need help..... I want last three digit number (RNG)...I HAVE PERVIOUS NUMBERS IF ANYONE CAN HELP ME.........PLEASE...... Waiting for the response.......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment