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

前端 未结 22 2381
攒了一身酷
攒了一身酷 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:50

    You can use this module https://www.npmjs.com/package/uid, it generates variable length unique id

    uid(10) => "hbswt489ts"
     uid() => "rhvtfnt" Defaults to 7
    

    Or you can have a look at this module https://www.npmjs.com/package/shortid

    const shortid = require('shortid');
    
    console.log(shortid.generate());
    // PPBqWA9
    

    Hope it works for you :)

提交回复
热议问题