Generate random string/characters in JavaScript

前端 未结 30 1748
闹比i
闹比i 2020-11-21 06:34

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?

相关标签:
30条回答
  • 2020-11-21 07:18

    let r = Math.random().toString(36).substring(7);
    console.log("random", r);

    Note: The above algorithm has the following weaknesses:

    • It will generate anywhere between 0 and 6 characters due to the fact that trailing zeros get removed when stringifying floating points.
    • It depends deeply on the algorithm used to stringify floating point numbers, which is horrifically complex. (See the paper "How to Print Floating-Point Numbers Accurately".)
    • Math.random() may produce predictable ("random-looking" but not really random) output depending on the implementation. The resulting string is not suitable when you need to guarantee uniqueness or unpredictability.
    • Even if it produced 6 uniformly random, unpredictable characters, you can expect to see a duplicate after generating only about 50,000 strings, due to the birthday paradox. (sqrt(36^6) = 46656)
    0 讨论(0)
  • 2020-11-21 07:18

    This works for sure

    <script language="javascript" type="text/javascript">
    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.substring(rnum,rnum+1);
     }
     document.randform.randomfield.value = randomstring;
    }
    </script>
    
    0 讨论(0)
  • 2020-11-21 07:19

    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:

    (Math.random().toString(36)+'00000000000000000').slice(2, N+2)
    

    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):

    Array(N+1).join((Math.random().toString(36)+'00000000000000000').slice(2, 18)).slice(0, N)
    

    Explanation:

    1. Pick a random number in the range [0,1), i.e. between 0 (inclusive) and 1 (exclusive).
    2. Convert the number to a base-36 string, i.e. using characters 0-9 and a-z.
    3. Pad with zeros (solves the first issue).
    4. Slice off the leading '0.' prefix and extra padding zeros.
    5. 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).
    6. Slice exactly N characters from the string.

    Further thoughts:

    • This solution does not use uppercase letters, but in almost all cases (no pun intended) it does not matter.
    • The maximum string length at N = 16 in the original answer is measured in Chrome. In Firefox it's N = 11. 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.
    • 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).
    • Not all possible strings of size N may be returned. 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. Specifically, if you look at the result of Math.random().toString(36), you'll notice the last character is not evenly distributed. 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. N=1) aren't affected.

    Update:

    Here are a couple other functional-style one-liners I came up with. They differ from the solution above in that:

    • They use an explicit arbitrary alphabet (more generic, and suitable to the original question which asked for both uppercase and lowercase letters).
    • All strings of length N have an equal probability of being returned (i.e. strings contain no repetitions).
    • They are based on a map function, rather than the toString(36) trick, which makes them more straightforward and easy to understand.

    So, say your alphabet of choice is

    var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    

    Then these two are equivalent to each other, so you can pick whichever is more intuitive to you:

    Array(N).join().split(',').map(function() { return s.charAt(Math.floor(Math.random() * s.length)); }).join('');
    

    and

    Array.apply(null, Array(N)).map(function() { return s.charAt(Math.floor(Math.random() * s.length)); }).join('');
    

    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.

    0 讨论(0)
  • 2020-11-21 07:22

    I know everyone has got it right already, but i felt like having a go at this one in the most lightweight way possible(light on code, not CPU):

    function rand(length, current) {
      current = current ? current : '';
      return length ? rand(--length, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".charAt(Math.floor(Math.random() * 60)) + current) : current;
    }
    
    console.log(rand(5));

    It takes a bit of time to wrap your head around, but I think it really shows how awesome javascript's syntax is.

    0 讨论(0)
  • 2020-11-21 07:23

    I did not find a clean solution for supporting both lowercase and uppercase characters.

    Lowercase only support:

    Math.random().toString(36).substr(2, 5)

    Building on that solution to support lowercase and uppercase:

    Math.random().toString(36).substr(2, 5).split('').map(c => Math.random() < 0.5 ? c.toUpperCase() : c).join('');

    Change the 5 in substr(2, 5) to adjust to the length you need.

    0 讨论(0)
  • 2020-11-21 07:25

    The most compact solution, because slice is shorter than substring. Subtracting from the end of the string allows to avoid floating point symbol generated by the random function:

    Math.random().toString(36).slice(-5);
    

    or even

    (+new Date).toString(36).slice(-5);
    

    Update: Added one more approach using btoa method:

    btoa(Math.random()).slice(0, 5);
    btoa(+new Date).slice(-7, -2);
    btoa(+new Date).substr(-7, 5);
    

    // Using Math.random and Base 36:
    console.log(Math.random().toString(36).slice(-5));
    
    // Using new Date and Base 36:
    console.log((+new Date).toString(36).slice(-5));
    
    // Using Math.random and Base 64 (btoa):
    console.log(btoa(Math.random()).slice(0, 5));
    
    // Using new Date and Base 64 (btoa):
    console.log(btoa(+new Date).slice(-7, -2));
    console.log(btoa(+new Date).substr(-7, 5));

    0 讨论(0)
提交回复
热议问题