Created
November 6, 2014 13:40
-
-
Save RavisMsk/b6f67036c4782981c5f1 to your computer and use it in GitHub Desktop.
Mongo - generate 2.5kk codes with hashcodes.
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
var symbols = '23456789QWERTYUPASDFGHJKZXCVBNM'; | |
var qty = 2500000; | |
var length = 7; | |
var code, hash, i, maxIndex, rand, symbol, _i, _j; | |
maxIndex = symbols.length - 1; | |
for (i = _i = 1; 1 <= qty ? _i <= qty : _i >= qty; i = 1 <= qty ? ++_i : --_i) { | |
code = ''; | |
hash = 0; | |
for (symbol = _j = 1; 1 <= length ? _j <= length : _j >= length; symbol = 1 <= length ? ++_j : --_j) { | |
rand = Math.floor((Math.random() * maxIndex) + 1); | |
code += symbols[rand]; | |
hash += rand; | |
if (hash > maxIndex) { | |
hash = hash - maxIndex; | |
} | |
} | |
code += symbols[hash]; | |
db.codes.insert({ | |
code: code, | |
hash: symbols[hash] | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment