Home > Web Front-end > JS Tutorial > How to generate random numbers in javascript_javascript skills

How to generate random numbers in javascript_javascript skills

WBOY
Release: 2016-05-16 16:47:58
Original
1747 people have browsed it

1. Use the built-in random number generation method:

Copy the code The code is as follows :

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

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

Copy code The code is as follows:

var now=new Date();
var number = now.getSeconds(); //This will generate an integer from 0 to 59 based on the current time.

var now=new Date();
var number = now.getSeconds()C; //This will generate an integer from 0 to 42 based on the current time.

3. A very excellent random number generator program that can be used in many fields.
Program code

Copy code The code is as follows:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template