-2

I use Math.random() right now, it returns some number like 0.7183306051883847. There're some bad sides of this function:

  • 0. at the beginning

  • no option to set a symbols length of the number

  • no option to add letters to the string

I wonder, what is the shortest code to generate strings like (expected characters length - 6 for example)?

  1. 2en81u
  2. 39438s
  3. ldksfn

Thanks.

Jasper
  • 5,090
  • 11
  • 34
  • 41

4 Answers4

10

This should be the shortest. If you need it uppercased, add an extra .toUpperCase() call at the tail.

Math.random().toString(36).substring(3,9)

Updated

This version is a bit longer, but it does avoid the problem that some special rational number may not have enough length.

(+new Date * Math.random()).toString(36).substring(0,6)  
  • 2
    http://stackoverflow.com/a/8084248/3110638 – Jonny 5 Dec 22 '13 at 11:31
  • @Jonny5 Aha. It seems that this is a widely used skill for generating random passwords extremely quickly. –  Dec 22 '13 at 11:38
  • Well, was the first, that google found :-) gertas comment from the linked thread might be integrated in this solution. – Jonny 5 Dec 22 '13 at 11:41
  • 1
    This can't possibly work in all cases. Math.random() can return any float value in the range, including values like 0.5, 0.25, and so on, that don't have enough digits when stringified. toString() will not add the extra 0s for you. The right thing to do is use the value itself--not the string representation of it--appropriately scaled and rounded, to index an array of characters. – Lee Daniel Crocker Dec 22 '13 at 17:34
  • @LeeDanielCrocker I got a new idea by multiplying a big interger (timestamp). –  Mar 13 '14 at 09:43
2

btoa does the job, but not particularly well:

> btoa(Math.random())
"MC43MzU0MzQ4NTk5OTEwNzM2"
> btoa(Math.random())
"MC44MTk2NzE0OTIzMjUxNDI="
> btoa(Math.random())
"MC41NDgwMzgxMzMxODcyMTk1"
> btoa(Math.random())
"MC4wNTk4NDc1NDIzMTc1ODQxNg=="
> btoa(Math.random())
"MC41NjA1NzYxNTEzNTc5NjM3"
> btoa(Math.random())
"MC4xMTA5MzY5ODY5ODA5NTk3Nw=="
> btoa(Math.random())
"MC42NDgwNzM5NjY2MTQ5MDI="
> btoa(Math.random())
"MC4zMTkzNTM2OTQzMjkwMzgyNg=="
> btoa(Math.random())
"MC4yOTU2OTgyOTE4NTQ5MzI5"
> btoa(Math.random())
"MC44ODc3MDEzNjQ0NjY5MjA1"

Note how they all start with MC4, since Math.random().toString() always starts with 0.

Eric
  • 95,302
  • 53
  • 242
  • 374
2
function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars[rnum];
    }
    return randomstring;
}
0

Get an array of ints that will be translated to your random string, something like this:

var myAscii = [],
    someAscii;
for (var i = 0; i < 5; i +=1) {
    someAscii = Math.floor(Math.random() * (90 - 65)) + 65; // This gives you a random number between 65 and 90, which is the upper case A-Z. Adapt it to your stuff.
    myAscii.push(someAscii);
}
var randomString = String.fromCharCode.apply(someAscii);

Anyway, it is a simpler approach to get a string with all the desired characters and perform a random extraction, as the related answer proposes.

bgusach
  • 14,527
  • 14
  • 51
  • 68