Last active
November 22, 2016 07:05
-
-
Save davidgilbertson/4ce2cc7e1deb809ea6f7 to your computer and use it in GitHub Desktop.
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 ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; | |
random_base64 = function random_base64(length) { | |
var str = ""; | |
for (var i=0; i < length; ++i) { | |
var rand = Math.floor(Math.random() * ALPHABET.length); | |
str += ALPHABET.substring(rand, rand+1); | |
} | |
return str; | |
} | |
var stringLength = 5; // because I don't have all day | |
var i; | |
var nums = []; | |
var safety = 100000; | |
console.log('Generating random strings with', Math.pow(ALPHABET.length, stringLength).toLocaleString(), 'combinations'); | |
for (i = 0; i < safety; i++) { | |
var num = random_base64(stringLength); | |
if (nums.indexOf(num) > -1) { | |
console.log(num, 'was generated twice after', i.toLocaleString(), 'tries'); | |
break; | |
} | |
nums.push(num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this in your console (or Chrome Dev Tools snippet). You'll get something interesting like this: