JS中实现种子随机数

参数:

详谈JS中实现种子随机数及作用

我在Egret里这么写...

class NumberTool{
    /**种子(任意默认值5)*/ public static seed:number = 5; /**种子随机数*/ public static seedRandom(){ this.seed = (this.seed * 9301 + 49297) % 233280; return this.seed / 233280.0; }
}

其中的 (this.seed * 9301 + 49297) % 233280 这些神秘数字参考“详谈JS中实现种子随机数及作用”吧,因为我也不知道...

执行种子随机数

//测试
for(let i=0;i<10;i++){
	console.log(NumberTool_Pao.seedRandom());
}

  

第一次输出:

扫描二维码关注公众号,回复: 5206597 查看本文章

 重新运行游戏,第二次输出,发现输出的随机数都是一样:

猜你喜欢

转载自www.cnblogs.com/gamedaybyday/p/10396955.html