0
点赞
收藏
分享

微信扫一扫

JS种子随机数实现方法,随手笔记

mjjackey 2022-01-24 阅读 66
javascript
  class rand {
    constructor(options) {
      let _options = {
        seed: Date.now(), // 随机种子
        be: 0 // 保留小数点
      }
      for (let b in options) _options[b] = options[b];
      this.seed = (_options.seed || Date.now()) % 999999999;
      this.be = _options.be;
    }

    // 取一个随机整数 max=最大值(0开始,不超过该值) 默认10
    next(max) {
      max = max || 10;
      this.seed = (this.seed * 9301 + 49297) % 233280;
      let val = this.seed / 233280.0;
      return Number((val * max).toFixed(this.be));
    }
  }

  const random = new rand();
  console.log(random.next());
举报

相关推荐

0 条评论