Home > Web Front-end > JS Tutorial > body text

js generates random numbers from 1 to 100

angryTom
Release: 2020-02-06 16:39:47
Original
13280 people have browsed it

js generates random numbers from 1 to 100

js generates random numbers from 1 to 100

js generates random numbers using the math.random() function

Math.random()
Copy after login

Specific implementation:

1. Define a random() function. The principle is to multiply the random number by the difference between the maximum value minus the minimum value and finally add the minimum value.

function random(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}
Copy after login

2. How to use

for (var i = 1; i <= 10; i++) {
    console.log(random(1, 100));
}
Copy after login

3. Rendering

js generates random numbers from 1 to 100

##This article comes from the

js tutorial column, welcome study!

The above is the detailed content of js generates random numbers from 1 to 100. For more information, please follow other related articles on the PHP Chinese website!

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