


All strings of length N have an equal probability of being returned (i.e.They use an explicit arbitrary alphabet (more generic, and suitable to the original question which asked for both uppercase and lowercase letters).

They differ from the solution above in that: Here are a couple other functional-style one-liners I came up with. Again, in almost all cases it does not matter, but we slice the final string from the beginning rather than the end of the random string so that short strings (e.g. Specifically, if you look at the result of Math.random().toString(36), you'll notice the last character is not evenly distributed. In the second solution this is obvious (since the smaller string is simply being duplicated), but also in the original answer this is true since in the conversion to base-36 the last few bits may not be part of the original random bits. Not all possible strings of size N may be returned.All returned strings have an equal probability of being returned, at least as far as the results returned by Math.random() are evenly distributed (this is not cryptographic-strength randomness, in any case).But as explained, the second solution is about supporting any requested string length, not about adding randomness, so it doesn't make much of a difference. The maximum string length at N = 16 in the original answer is measured in Chrome.This solution does not use uppercase letters, but in almost all cases (no pun intended) it does not matter.Slice exactly N characters from the string.Repeat the string enough times to have at least N characters in it (by Joining empty strings with the shorter random string used as the delimiter).Slice off the leading '0.' prefix and extra padding zeros.Pad with zeros (solves the first issue).Convert the number to a base-36 string, i.e.Pick a random number in the range [0,1), i.e.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): Array(N+1).join((Math.random().toString(36)+'00000000000000000').slice(2, 18)).slice(0, N) Second, both the original and the solution above limit the string size N to 16 characters. The original has two drawbacks which are addressed here:įirst, 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's an improvement on doubletap's excellent answer. TypeError: Crypto.getRandomValues requires at least 1 argument, but only 0 were passedįor IE11 support you can use - (window.crypto || window.msCrypto).getRandomValues(arr) Here's a little console example - > var arr = new Uint8Array(4) # make array of 4 bytes (values 0-255) The array given as the parameter is filled with random numbers (random in its cryptographic meaning). The crypto.getRandomValues() method lets you get cryptographically strong random values. "82defcf324571e70b0521d79cce2bf3fffccd69"įor more information on crypto.getRandomValues. Var arr = new Uint8Array((len || 40) / 2)
