Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created January 2, 2014 16:59
Show Gist options
  • Save Protonk/8222366 to your computer and use it in GitHub Desktop.
Save Protonk/8222366 to your computer and use it in GitHub Desktop.
The below is released under the GPL http://www.gnu.org/licenses/gpl.html
//Webkit2's crazy invertible mapping generator
// Theory is here: http://dl.acm.org/citation.cfm?id=752741
var xorshift = (function(seed) {
// We can't reach the maximal period inside JS because numbers above
// 2^53 aren't 'safe'
seed = seed || Math.round(Math.random() * Math.pow(2, 32));
return {
rand : function() {
// creates randomness...somehow...
seed += (seed * seed) | 5;
// Shift off bits, discarding the sign. Discarding the sign is
// important because OR w/ 5 can give us + or - numbers.
return (seed >>> 32) / max;
}
};
}(seed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment