Dan*_*mbe 18 javascript random math
我目前正在使用JavaScript制作康威的生命游戏,我注意到函数Math.random()总是返回某种模式.以下是100x100网格中随机结果的示例:

有谁知道如何获得更好的随机数字?
ApplyRandom: function() {
var $this = Evolution;
var total = $this.Settings.grid_x * $this.Settings.grid_y;
var range = parseInt(total * ($this.Settings.randomPercentage / 100));
for(var i = 0; i < total; i++) {
$this.Infos.grid[i] = false;
}
for(var i = 0; i < range; i++) {
var random = Math.floor((Math.random() * total) + 1);
$this.Infos.grid[random] = true;
}
$this.PrintGrid();
},
Run Code Online (Sandbox Code Playgroud)
[UPDATE]
我在这里创建了一个jsFiddle:http://jsfiddle.net/5Xrs7/1/
[UPDATE]
似乎Math.random()毕竟是好的(感谢raina77ow).对不起人!:(.如果你对结果感兴趣,这里是游戏的更新版本:http://jsfiddle.net/sAKFQ/
(但我认为还有一些漏洞......)
rai*_*7ow 11
你的代码中的这一行......
var position = (y * 10) + x;
Run Code Online (Sandbox Code Playgroud)
......是导致这种"非随机性"的原因.真的应该......
var position = (y * $this.Settings.grid_x) + x;
Run Code Online (Sandbox Code Playgroud)
我想这10是这个网格的原始大小,这就是为什么它在这里.但这显然是错误的:你应该根据网格的当前大小选择你的位置.
作为旁注,没有冒犯,但我仍然认为@JayC答案中给出的算法优于你的.并且它很容易实现,只需将ApplyRandom函数中的两个循环更改为单个循环:
var bias = $this.Settings.randomPercentage / 100;
for (var i = 0; i < total; i++) {
$this.Infos.grid[i] = Math.random() < bias;
}
Run Code Online (Sandbox Code Playgroud)
通过此更改,您将不再受到重复使用相同数字的副作用var random = Math.floor((Math.random() * total) + 1);,这会降低原始代码中的实际单元格填充率.
| 归档时间: |
|
| 查看次数: |
5401 次 |
| 最近记录: |