JavaScript can use JavaScriptMath. random () built-in function to generate random numbers. The following is a good example. You can try to operate the JavaScript Math. random () built-in function.
Random Function Return Value
Returns a pseudo-random number between 0 and 1, which may be 0, but always less than 1)
Random Function example
// Returns a random number.
Document. write (Math. random ());
// Returns a random number ranging from 10 to 20.
Document. write (Math. random () * (20-10) + 10 );
// Return the formula of the random number (m-n) in the specified range
Document. write (Math. random () * (n-m) + m );
Time-based 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.