Home >Web Front-end >JS Tutorial >js generates random numbers from 1 to 100

js generates random numbers from 1 to 100

angryTom
angryTomOriginal
2020-02-06 16:39:4713330browse

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()

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;
}

2. How to use

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

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!

Statement:
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
Previous article:Understanding MVVMNext article:Understanding MVVM