Skip to content

Instantly share code, notes, and snippets.

@mcenirm
Last active December 12, 2015 00:58
Show Gist options
  • Save mcenirm/4687301 to your computer and use it in GitHub Desktop.
Save mcenirm/4687301 to your computer and use it in GitHub Desktop.
Bad random password generator (Must be at least 8 characters, contain at least one uppercase letter, one lowercase letter, one number and one symbol.)
<!DOCTYPE html>
<a href='://bl.ocks.org/4687301'>bl.ocks.org/4687301</a>
<button id='another'>Another one</button>
<span id='password'></span>
<script>
// return random integer in [0,n-1]
function r(n) {
return Math.floor(n * Math.random());
}
// return random index from array
function rr(a) {
return r(a.length);
}
// return random item from array
function rrr(a) {
return a[rr(a)];
}
// generate a bad random password
function bad_random_password() {
var N = 8;
var X = [
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
'abcdefghijklmnopqrstuvwxyz'.split(''),
'0123456789'.split(''),
//'~!@#$%^&*()_+`-={}|[]\\:";\'<>?,./'.split(''),
'$%&*@#!_:~'.split(''),
];
var s = '';
var i = N;
var used = X.map(function(){return false;});
while (i > 0) {
var which = rr(X);
var chars = X[which];
s += rrr(chars);
i--;
if (used[which] && i < X.length) {
X.splice(which,1);
used.splice(which,1);
} else {
used[which] = true;
}
}
return s;
}
(function () {
var x = document.getElementById('password');
var a = document.getElementById('another');
a.onclick = function() {
x.textContent = bad_random_password();
};
a.click();
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment