I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]
.
What's the best way to do this with JavaScript?
I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]
.
What's the best way to do this with JavaScript?
How about this compact little trick?
You need the
Array.apply
there to trick the empty array into being an array of undefineds.If you're coding for ES2015, then building the array is a little simpler:
You can use coderain. It's a library to generate random codes according to given pattern. Use
#
as a placeholder for upper and lowercase characters as well as digits:There are other placeholders like
A
for uppercase letters or9
for digits.What may be useful is that calling
.next()
will always give you a unique result so you don't have to worry about duplicates.Here is a demo application that generates a list of unique random codes.
Full disclosure: I'm the author of coderain.
Random String Generator (Alpha-Numeric | Alpha | Numeric)
Have fun. jsBin demo
While the above uses additional checks for the desired (A/N, A, N) output, let's break it down the to the essentials (Alpha-Numeric only) for a better understanding:
var str = "";
to concatenate random charactersrand
index number from 0 to 61 (0..9+A..Z+a..z = 62)rand
(since it's 0..61) incrementing it by some number (see examples below) to get back the rightCharCode
number and the related Character.str
aString.fromCharCode( incremented rand )
Let's picture the Character table and their ranges:
Math.floor( Math.random * 62 )
gives a range from0..61
(what we need). How to fix (increment) the random to get the correct charCode ranges?The conditional operation logic from the table above:
If you followed the above explanation you should be able to create this alpha-numeric snippet:
jsBin demo
Or if you will:
Here's an improvement on doubletap's excellent answer. The original has two drawbacks which are addressed here:
First, as others have mentioned, it has a small probability of producing short strings or even an empty string (if the random number is 0), which may break your application. Here is a solution:
Second, both the original and the solution above limit the string size N to 16 characters. The following will return a string of size N for any N (but note that using N > 16 will not increase the randomness or decrease the probability of collisions):
Explanation:
Further thoughts:
Update:
Here are a couple other functional-style one-liners I came up with. They differ from the solution above in that:
So, say your alphabet of choice is
Then these two are equivalent to each other, so you can pick whichever is more intuitive to you:
and
Edit:
I seems like qubyte and Martijn de Milliano came up with solutions similar to the latter (kudos!), which I somehow missed. Since they don't look as short at a glance, I'll leave it here anyway in case someone really wants a one-liner :-)
Also, replaced 'new Array' with 'Array' in all solutions to shave off a few more bytes.
Here are some easy one liners. Change
new Array(5)
to set the length.Including
0-9a-z
Including
0-9a-zA-Z
Here's the method I created.
It will create a string containing both uppercase and lowercase characters.
In addition I've included the function that will created an alphanumeric string too.
Working examples:
http://jsfiddle.net/greatbigmassive/vhsxs/ (alpha only)
http://jsfiddle.net/greatbigmassive/PJwg8/ (alphanumeric)
Upgrade July 2015
This does the same thing but makes more sense and includes all letters.