How to Generate a random number of fixed length using JavaScript?

前端 未结 22 2362
攒了一身酷
攒了一身酷 2021-01-30 10:23

I\'m trying to generate a random number that must have a fixed length of exactly 6 digits.

I don\'t know if JavaScript has given below would ever create a number less th

22条回答
  •  被撕碎了的回忆
    2021-01-30 10:40

    npm install --save randomatic
    
    var randomize = require('randomatic');
    randomize(pattern, length, options);
    

    Example:

    To generate a 10-character randomized string using all available characters:

    randomize('*', 10);
    //=> 'x2_^-5_T[$'
    
    randomize('Aa0!', 10);
    //=> 'LV3u~BSGhw'
    

    a: Lowercase alpha characters (abcdefghijklmnopqrstuvwxyz'

    A: Uppercase alpha characters (ABCDEFGHIJKLMNOPQRSTUVWXYZ')

    0: Numeric characters (0123456789')

    !: Special characters (~!@#$%^&()_+-={}[];\',.)

    *: All characters (all of the above combined)

    ?: Custom characters (pass a string of custom characters to the options)

    NPM repo

提交回复
热议问题