-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.

3
  • 1
    I am sorry, were you looking for codegolf.stackexchange.com? Commented Dec 22, 2013 at 11:17
  • @elclanrs answer is inside the question Commented Dec 22, 2013 at 11:24
  • 1
    @Steve: "What do you want" and "What have you tried" are not the same thing Commented Dec 22, 2013 at 11:25

4 Answers 4

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

@Jonny5 Aha. It seems that this is a widely used skill for generating random passwords extremely quickly.
Well, was the first, that google found :-) gertas comment from the linked thread might be integrated in this solution.
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.
@LeeDanielCrocker I got a new idea by multiplying a big interger (timestamp).
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.

3 Comments

There're unwanted == at the end and uppercased letters.
@steve: .toLower().replace(/=/g, '')
Also, btoa isn't supported by ie9, only ie10+
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; } 

6 Comments

chars.substring(rnum, rnum + 1) => chars[rnum]
thanks, but this function is too long.
@Steve: Please, explain how a piece of code can be "too long"?
I don't understand. where does 'the shortest code logic' come from? This is a good function and the chance of returning duplicate word is very low.
Regarding, the accepted answer will be much longer when you need let's say 60 char long strings, and this still fits, I gave my vote for this answer.
|
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.

5 Comments

Never name your variables as char
Actually, I don't need maxNumber option, I need maxSymbols option instead
@thefourtheye: Why not?
fromCharCode is nothing, since it doesn't give a random string with an expected length of symbols
Doesn't work. Why are you passing for var someAscii last value to String.fromCharCode?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.