doubingqi5829 2018-06-19 00:24
浏览 99
已采纳

PHP伪随机数生成器

I am using a linear conguential generator in my javascript code but now I need to validate the results server side (generate the same numbers from the same seed). I have translated my javascript code in PHP but it does not work as expected. The first few numbers are close to the javascript ones but with less precision and the sequence contains some negative numbers which are not present in the javascript version. I think this is because of PHP's different floating point precision but I am confused by the negative numbers.

If there is no easy way to make this work in PHP what other methods could I use to generate the same sequence of pseudo-random numbers both in javascript and in PHP?

Javascript

function SeededRandom(newSeed) {
    this.seed = newSeed;
    this.Random = function (min, max) {
        this.seed = (this.seed * 9301 + 49297) % 233280;
        return Math.floor(min + (this.seed / 233280) * (max - min + 1));
    }
}

PHP

class SeededRandom {
    private $seed;
    public function __construct($newSeed) {
        $this->seed = $newSeed;
    }
    public function Random($min, $max) {
        $this->seed = ($this->seed * 9301 + 49297) % 233280;
        return floor($min + ($this->seed / 233280) * ($max - $min + 1));
    }
}
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 内存管理的一段代码不是很理解
      • ¥20 打开anaconda时卡在Loading applications无法进入界面
      • ¥15 网页超时时间设置失效
      • ¥15 有关绿色信贷毕业论文的问题
      • ¥30 关于#机器人#的问题,如何解决?
      • ¥15 求MATLAB函数ScalarLayerDisplay的代码
      • ¥15 安卓如何自动执行检测到的NFC标签,无需再点确认
      • ¥15 pyHM库mouse模块的ValueError错误
      • ¥15 python opencv 摄像头 传感器
      • ¥30 eMMC&Android&C&Linux