There are so many ways to generate random strings in JavaScript and it doesn't really matter which method is faster.
The method I like using most is Math.random()
I made a video on it:
Basically the idea is to use Math.random(), then you can convert it to string and do some simple string manipulation on it.
To get random numbers, I would use something like below:
Math.ceil(Math.random()*10000)
To get random strings with numbers only, I would use:
Math.random().toString().substr(2, 5)
Fortunate .toString() has a param called radix that you can pass in numbers between 2 - 36 which will cast the generated numbers to the radix characters that fall between the given number. The radix is also known as base and its for representing numeric values
To get a random number between 0-1:
Math.random().toString(2).substr(2, 5)
To get a random number between 0-5:
Math.random().toString(5).substr(2, 5)
Starting from 11/12, it will start introducing letters. So to get a fully random string:
Math.random().toString(20).substr(2, 6)
With this you can now write your awesome random string generator:
const generateRandomString = function(){
return Math.random().toString(20).substr(2, 6)
}
To be able to change the length of the output:
const generateRandomString = function(length=6){
return Math.random().toString(20).substr(2, length)
}
One liner
const generateRandomString = (length=6)=>Math.random().toString(20).substr(2, length)
That's all.
If you know of any other faster ways, please I would love to see it in the comment section.
Thanks
Top comments (8)
Nice function! But one thing I noticed is that this will only work for random alpha-numeric strings up to a certain length. If you want to have something longer, you should call the method recursively. Something like:
Yep, looks like it tops out at 12-13 characters for me, demo here: jsfiddle.net/mberwk8a/2/
Hi, thanks for the simple, straight-forward article! I just wanted to point out a small error in your one-liner. You define the
lengthvariable, but you still used a hard-coded "6" instead of it in the expression.Do you like base20 for something special?
No reason actually, I just like using it.
Just to clarify for people who don't know, the radix argument for
toStringgoes from 2 to 36, and you need to use 36 to include all alphanumeric characters in the alphabet. Using 20 will omit more than half of the alphabetic letters from the output.Don't use the
.substr(2, ...)at the end, because you risk having empty string as a result. The result ofMath.random()can be0, so if yousubstr(2, ...), you will end up with an empty string.You asked for faster so here you go, ~10% faster with the same memory usage:
The charset of this function is increased from the default
0-9, a-jto0-9, a-zas a side-effect.Please note that I would advise against using this method though. This method can, worst case, return an empty string if
Math.randomreturns0. Sure, rare, but(0.36336772809975615).toString(36).slice(2).lengthis 5. My recommendation, which is ~10% slower than your version:The cryptographically safe variant is ~11x slower:
Bear in mind that the random numbers by
getRandomValuesare going to be between 0 and 255 (inclusive), so I'm using modulo (%) to get the remainder, which results in some letters having a higher appearance probability than others, specifically a, b, c and d in this case. Best case would be a charset length which 256 is divisible by, meaning 2, 4, 8, 16, 32, 64, 128 or 256, where there are no duplicate characters.