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

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

    Based on link you've provided, right answer should be

    Math.floor(Math.random()*899999+100000);

    Math.random() returns float between 0 and 1, so minimum number will be 100000, max - 999999. Exactly 6 digits, as you wanted :)

提交回复
热议问题