Created
March 16, 2011 09:46
-
-
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
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
function(){var a=1E15;return function(){var b=~~(1E3*Math.random());a>9E15-b&&(a=1E15);a+=b;return a.toString(36)}}(); |
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
// 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