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

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

    console.log(Math.floor(100000 + Math.random() * 900000));

    Will always create a number of 6 digits and it ensures the first digit will never be 0. The code in your question will create a number of less than 6 digits.

提交回复
热议问题