代码片段
1 行
js怎么输出30-50之间的随机数
JavaScript
5 行
function get(){return Math.round(Math.random()*20+30)}
document.write(get()+",")
document.write(get()+",")
document.write(get()+",")
document.write(get()+",")
代码片段
1 行
怎么求1~10之间的随机数 js
代码片段
1 行
Math.round(Math.random()*9+1);

Math.round和Math.random取得随机数

那你要去的随机数的范围是 多少到多少呢?
比如 :你需要去的随机数为0-9,每个概率为1/10 就这样写

代码片段
1 行
Math.round(Math.random()*9) 

如果你是想要65-70,每个概率为1/6,(65,66,67,68,69,70一共6个数)
就这样写

代码片段
1 行
Math.round(Math.random()*5)+65

如果你需要 1/10000 的概率的话就是你上面的那种

代码片段
1 行
Math.round(Math.random()*9999)

而你想要的是1/10000的概率,但你需要的数字要小于1000(小于3位数字)

代码片段
1 行
num=Math.round(Math.random()*9999)

循环开始 1=1 (代码忘记了不写了你自己写无限循环)

代码片段
6 行
if(num>1000 ){
    num=Math.round(Math.random()*9999)
}else{
    退出循环命令

}

结束循环
这样就可以获得低于4位的数字了 如果要求 需要几位数字可以再 if条件里面自己加 条件
比如 NUM>1000 and num < 99
他就获取3数字(100-999)