Implementing random numbers is a common programming task in various programming languages. The following describes how to implement random numbers in JavaScript. I hope you will like this simple and practical example. Today, some netizens asked me how to generate a random number in the specified range in JavaScript. Math. random () is used to generate random numbers. However, the general reference manual does not explain how to use this method to generate random numbers within a specified range. This time I will introduce Math. random () in detail and how to use it to generate random numbers within the specified range.

For the basic tutorial, see here

Http://www.jb51.net/w3school/js/jsref_random.htm

After reading the tutorial, you should know the basic usage of the Math. random () method.

Rounding data using parseInt (), Math. floor (), or Math. ceil ()

We can see that the Math. random () method is used directly to generate a number smaller than 1, so:

Math.random()*5

The result is a random number less than 5. What we usually want to get is an integer between 0 and 5, so we need to round the result to get the expected integer. ParseInt (), Math. floor (), and Math. ceil () can all play a rounding role.

var randomNum = Math.random()*5;alert(randomNum); // 2.9045290905811183 alert(parseInt(randomNum,10)); // 2alert(Math.floor(randomNum)); // 2alert(Math.ceil(randomNum)); // 3

From the test code, we can see that the effects of parseInt () and Math. floor () are the same, all of which are down integers. So parseInt (Math. random () * 5, 10) and Math. floor (Math. random () * 5) is a random number between 0 and 4, Math. ceil (Math. random () * 5) is a random number between 1 and 5.

**Generate random numbers within a specified range**

Therefore, if you want to generate a random number ranging from 1 to any value, the formula is as follows:

// Max-the expected maximum value parseInt (Math. random () * max, 10) + 1; Math. floor (Math. random () * max) + 1; Math. ceil (Math. random () * max );

If you want to generate a random number ranging from 0 to any value, the formula is as follows:

// Max-the expected maximum value parseInt (Math. random () * (max + 1), 10); Math. floor (Math. random () * (max + 1 ));

If you want to generate a random number from any value to any value, the formula is as follows:

// Max-the expected maximum value // min-the expected minimum value parseInt (Math. random () * (max-min + 1) + min, 10); Math. floor (Math. random () * (max-min + 1) + min );

**Next let's take a look at other methods for generating random numbers using javascript.**

1. Use the built-in Random Number Generation Method: (as mentioned earlier, here is a brief description)

Math. random (); // This method generates a floating point number between 0 and 1. Math. floor (Math. random () * 10 + 1); // 1-10Math.floor (Math. random () * 24); // 0-23

2. Based on Time, random numbers can also be generated:

__ __The Code is as follows:

Var now = new Date ();

Var number = now. getSeconds (); // This generates an integer ranging from 0 to 59 based on the current time.

Var now = new Date ();

Var number = now. getSeconds () % 43; // This generates an integer ranging from 0 to 42 based on the current time.

3. A fairly good random number generator program can be used in many fields.