Skip to content

Instantly share code, notes, and snippets.

@netroy
Created March 16, 2011 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netroy/872242 to your computer and use it in GitHub Desktop.
Save netroy/872242 to your computer and use it in GitHub Desktop.
Pseudo-random Token generator .. dont have to worry about uniques. Coded for nodejs ... outputs to console the generated tokens.. can be used as IDs in any application
function(){var a=1E15;return function(){var b=~~(1E3*Math.random());a>9E15-b&&(a=1E15);a+=b;return a.toString(36)}}();
// This produces (9e15-1e15)/1e3 tokens in worst case,
// i.e. 8e12 or 8000 billions.... thats got to be more than enough
var randomGenerator = (function(){
var MAX = 9e15;
var MIN = 1e15;
var safegap = 1000;
var counter = MIN;
return function(){
var increment = Math.floor(safegap*Math.random());
if(counter > (MAX - increment)) counter = MIN;
counter += increment;
return counter.toString(36);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment