正文 1016字数 107,748阅读

js怎么输出30-50之间的随机数
Run code
Cut to clipboard

    function get(){return Math.round(Math.random()*20+30)} document.write(get()+",") document.write(get()+",") document.write(get()+",") document.write(get()+",")
    Run code
    Cut to clipboard

      怎么求1~10之间的随机数 js
      Run code
      Cut to clipboard

        Math.round(Math.random()*9+1);
        Run code
        Cut to clipboard

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

          那你要去的随机数的范围是 多少到多少呢?
          比如 :你需要去的随机数为0-9,每个概率为1/10 就这样写
          Math.round(Math.random()*9)
          Run code
          Cut to clipboard

            如果你是想要65-70,每个概率为1/6,(65,66,67,68,69,70一共6个数)
            就这样写
            Math.round(Math.random()*5)+65
            Run code
            Cut to clipboard

              如果你需要 1/10000 的概率的话就是你上面的那种
              Math.round(Math.random()*9999)
              Run code
              Cut to clipboard

                而你想要的是1/10000的概率,但你需要的数字要小于1000(小于3位数字)
                num=Math.round(Math.random()*9999)
                Run code
                Cut to clipboard

                  循环开始 1=1 (代码忘记了不写了你自己写无限循环)
                  if(num>1000 ){ num=Math.round(Math.random()*9999) }else{ 退出循环命令 }
                  Run code
                  Cut to clipboard

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