Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created April 13, 2011 14:25
Show Gist options
  • Save oberhamsi/917633 to your computer and use it in GitHub Desktop.
Save oberhamsi/917633 to your computer and use it in GitHub Desktop.
random number generator, as seen on michalbe's blog
/**
* ent 8? don't use
*
* @example
*
* var rng = new CustomRandom();
* rng.next();
* rng.next(2, 100);
*
* @see http://michalbe.blogspot.com/2011/02/javascript-random-numbers-with-custom_23.html
*/
function CustomRand (nseed) {
var seed;
var constant = Math.pow(2, 13)+1;
var prime = 1987;
var maximum = 1000;
var seed = nseed || (new Date()).getTime();
return {
next: function(min, max) {
seed *= constant;
seed += prime;
seed %= maximum;
return min && max ? min+seed/maximum*(max-min) : seed/maximum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment