JS generates a random number of the specified bits

Source: Internet
Author: User

< HTML >
<script>//Gets the random number of the specified number of bits function getrandom (num) { var random = Math.floor ((Math.random () +math.floor ( Math.random () *9+1)) *math.pow (10,num-1));} Call the random number function to generate a 10-digit random number getrandom (10);
</script></html>

Implementation of the idea (at the end of the code process and the results of the operation) to obtain a 10-bit random number as an example:

1, Math.random () function can get 0 to 1 decimal, Math.pow (10,10) function is equivalent to 10 10, the Math.floor () function is rounded down to remove the decimal place;

2, the combination can obtain a 10-bit random number: Math.floor (Math.random () *math.pow (10,10));

3, however, if the first decimal digit of Math.randow () is 0, it is possible to obtain a 9-bit random number;

4, the Math.randow () plus 1, excluding the first decimal place for the case of 0, the corresponding power operation minus one

Math.floor ((Math.random () +1)) *math.pow (10,9));

This will get a random number of 10 bits, but all will start with 1;

5, in order to start also can randomly take number, can replace 1 with Math.floor (Math.random () *9+1);

6. The final code is as follows:

Math.floor ((Math.random () +math.floor (Math.random () *9+1)) *math.pow (10,9));

So we can get a completely random 10-bit random number.

Gets the random number, the first digit of the decimal may be 0console.log (Math.random ());//Gets a 10-bit random number, if the decimal first bit is 0 then only 9 digits Console.log (Math.floor (Math.random () * Math.pow (10,10)));//random number +1, solving the situation where the first digit of the decimal is 0//But the first digit that causes the random number is always 1console.log (Math.floor ((Math.random () +1) *math.pow (10,9 )), or//The random number +1 is also changed to random plus the number between 1~9, solve the first bit is always 1 problem Console.log (Math.floor ((Math.random () +math.floor (Math.random () *9+1) * Math.pow (10,9)); Chrome browser Debug Run results-----------------------------------------------------------------------------get random numbers, The first digit of the decimal may be 0:0.097574709201919 to get 10-bit random numbers, if the decimal number first is 0 then only 9 digits: 623721160 random number + 1, solve the situation where the first digit of the decimal is 0: But the first digit of the random number is always 1:1,242,782,126, the random number +1 is also changed to random plus the number between 1~9, solving the problem that the first bit is always 1:7671051679

JS generates a random number of the specified bits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.