Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function randomChrs(len) {
- // Just an array of the characters we want in our random string
- var chrs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
- // Check that a length has been supplied and if not default to 32
- len = (isNaN(len)) ? 32 : len;
- // The following section shuffles the array just to further randomise the output
- var tmp, current, top = chrs.length;
- if(top)
- {
- while(--top)
- {
- current = Math.floor(Math.random() * (top + 1));
- tmp = chrs[current];
- chrs[current] = chrs[top];
- chrs[top] = tmp;
- }
- }
- // Just a holder for our random string
- var randomStr = '';
- // Loop through the required number of characters grabbing one at random from the array each time
- for(i=0;i<len;i++)
- {
- randomStr = randomStr + chrs[Math.floor(Math.random()*chrs.length)];
- }
- // Return our random string
- return randomStr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement