Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var codes = generateUniqueCodes(10000000);
- var query = printQuery(codes);
- console.log(query);
- function unique(arr) {
- var sorted = arr;
- sorted.sort();
- return sorted.filter(function(value, index, arr){
- if(index < 1)
- return true;
- else
- return value != arr[index-1];
- });
- }
- function printQuery(codes) {
- var query = 'INSERT INTO referral_code(code) VALUES '
- for(var i = 0; i < codes.length; i++) {
- query += '(\'RF' + codes[i] + '\'),';
- }
- return query.substring(0,query.length - 1) + ';';
- }
- function generateUniqueCodes(n) {
- var codes = [];
- for (var i = 1; i <= n; i++) {
- codes.push(generate(5));
- }
- //codes = codes.filter(onlyUnique);
- return unique(codes);
- }
- function generate(limit) {
- var dictionary = 'qwertyuiopasdfghjklzxcvbnm1234567890';
- var maxOffset = dictionary.length;
- var code = '';
- for (var length = limit; length > 0; length--) {
- code += dictionary[Math.floor(Math.random() * maxOffset)];
- }
- return code;
- }
- function onlyUnique(value, index, self) {
- return self.indexOf(value) === index;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement