Created
April 13, 2011 14:25
-
-
Save oberhamsi/917633 to your computer and use it in GitHub Desktop.
random number generator, as seen on michalbe's blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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