请选择 进入手机版 | 继续访问电脑版
开启辅助访问
帐号登录 |立即注册

如何使用JS生成随机数

 
Math. random( )方法
从0-1之间产生一个随机数,取值范围:>=0 and  <1

案例:产生0-5之间随机数
//0~ 5   取值范围:(0~0.9.9999..)*5
生成0-max的随机数公式:
Math. floor(Math. random( )*(Max+1));
生成0-5之间随机数,则
console. log(Math. floor(Math. random( )* (5 + 1)));

案例:产生一个区间的随机数
//2~ 5   取值范围:2+(0~0.9.9999..)*3
生成X-max的随机数公式:
min+Math. floor(Math. random( )*(Max-min+1));
console. log(2 + Math. floor(Math. random()*(5 - 2 + 1)));

回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则