繁体   English   中英

在JavaScript中生成多个随机数

[英]Generate multiple random numbers in JavaScript

我正在尝试为轮盘赌系统编写脚本,并且希望脚本运行直到bank变量达到0或11,000,并在每次旋转后生成三段数据。

为了简单起见,我省略了部分代码。 if else语句中的代码不是问题。 运行脚本,直到变量达到某个特定点为止,这就是我遇到的问题。

任何人都可以帮助我重写此脚本吗? 提前致谢。

(function() {
  var bank = 10000;
  var bet = 1;

  function spin() {
    var number = Math.floor(Math.random() * 36);

    if ( number == 0 ) {
      document.write("<p>The number " + number + " is neither high nor low.</p>");
      // removed
    } 
    else if ( number > 18 ) {
      document.write("<p>The number " + number + " is a high number.</p>");
      // removed
    }
    else {
      document.write("<p>The number " + number + " is a low number.</p>");
      // removed
    }
  };

  spin();

  document.write("<p>Total bank is now " + bank + ".</p>");
  document.write("<p>The next bet is " + bet + ".</p>");

})();

如果要在页面内调用此循环(即,您不希望页面在持续时间内挂断,或不想显示结果),则应使用requestAnimationFrame

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

然后将您的循环设置为仅在银行在您接受的范围内时才自行呼叫:

function spinLoop() {
    spin();

    //perform your UI updates here

    if (bank >= 0 && bank <= 11000) requestAnimFrame(spinLoop);
}

当然,您首先需要调用此函数来启动循环:

spinLoop();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM