Home  >  Article  >  Web Front-end  >  js generates random numbers within a certain range

js generates random numbers within a certain range

王林
王林forward
2020-05-21 09:27:573042browse

js generates random numbers within a certain range

You can use the random() method, which returns a random number between 0 ~ 1.

The specific code is as follows:

//生成最小min到最大max的随机数
function random(min,max){
        if(min > max){
            var ls = min;
            min = max;
            max = ls;
        }
        return Math.floor(Math.random() * (max-min+1) ) + min;
    }

Recommended tutorial: js entry tutorial

The above is the detailed content of js generates random numbers within a certain range. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete