c00*_*0fd 22 javascript algorithm math probability
比如说,如果我想在min和之间生成一个无偏的随机数max,我会这样做:
var rand = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要的生成之间的随机数min,并max更偏向价值N之间min并max在一定程度上D?最好用概率曲线来说明它:

小智 31
这是一种方式:
即,伪:
Variables: min = 0 max = 100 bias = 67 (N) influence = 1 (D) [0.0, 1.0] Formula: rnd = random() x (max - min) + min mix = random() x influence value = rnd x (1 - mix) + bias x mix
混合因子可以通过次要因子来减少,以设置它应该影响多少(即mix * factor,因子是[0,1]).
这将绘制一个有偏差的随机范围.上部带有1个影响,最低0.75影响.此处的偏差设定在该范围内的2/3位置.底部带没有(故意)偏见进行比较.
var ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "red"; ctx.fillRect(399,0,2,110); // draw bias target
ctx.fillStyle = "rgba(0,0,0,0.07)";
function getRndBias(min, max, bias, influence) {
var rnd = Math.random() * (max - min) + min, // random in range
mix = Math.random() * influence; // random mixer
return rnd * (1 - mix) + bias * mix; // mix full range and bias
}
// plot biased result
(function loop() {
for(var i = 0; i < 5; i++) { // just sub-frames (speedier plot)
ctx.fillRect( getRndBias(0, 600, 400, 1.00), 4, 2, 50);
ctx.fillRect( getRndBias(0, 600, 400, 0.75), 55, 2, 50);
ctx.fillRect( Math.random() * 600 ,115, 2, 35);
}
requestAnimationFrame(loop);
})();Run Code Online (Sandbox Code Playgroud)
<canvas width=600></canvas>Run Code Online (Sandbox Code Playgroud)
只是为了好玩,这里有一个依赖于高斯函数的版本,如 SpiderPig 对您的问题的评论中所述。高斯函数应用于 1 到 100 之间的随机数,其中钟形的高度表示最终值与 的接近程度N。我将度数解释为D最终值接近 的可能性N,因此D对应于钟形的宽度 - 越小D,偏差的可能性越小。显然,该示例可以进一步校准。
(我复制了 Ken Fyrstenberg 的画布方法来演示该功能。)
function randBias(min, max, N, D) {
var a = 1,
b = 50,
c = D;
var influence = Math.floor(Math.random() * (101)),
x = Math.floor(Math.random() * (max - min + 1)) + min;
return x > N
? x + Math.floor(gauss(influence) * (N - x))
: x - Math.floor(gauss(influence) * (x - N));
function gauss(x) {
return a * Math.exp(-(x - b) * (x - b) / (2 * c * c));
}
}
var ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(399, 0, 2, 110);
ctx.fillStyle = "rgba(0,0,0,0.07)";
(function loop() {
for (var i = 0; i < 5; i++) {
ctx.fillRect(randBias(0, 600, 400, 50), 4, 2, 50);
ctx.fillRect(randBias(0, 600, 400, 10), 55, 2, 50);
ctx.fillRect(Math.random() * 600, 115, 2, 35);
}
requestAnimationFrame(loop);
})();Run Code Online (Sandbox Code Playgroud)
<canvas width=600></canvas>Run Code Online (Sandbox Code Playgroud)
乐趣:使用图像作为密度函数。对随机像素进行采样,直到得到黑色像素,然后取 x 坐标。

代码:
getPixels = require("get-pixels"); // npm install get-pixels
getPixels("distribution.png", function(err, pixels) {
var height, r, s, width, x, y;
if (err) {
return;
}
width = pixels.shape[0];
height = pixels.shape[1];
while (pixels.get(x, y, 0) !== 0) {
r = Math.random();
s = Math.random();
x = Math.floor(r * width);
y = Math.floor(s * height);
}
return console.log(r);
});
Run Code Online (Sandbox Code Playgroud)
示例输出:
0.7892316638026386
0.8595335511490703
0.5459279934875667
0.9044852438382804
0.35129814594984055
0.5352215224411339
0.8271261665504426
0.4871773284394294
0.8202084102667868
0.39301465335302055
Run Code Online (Sandbox Code Playgroud)
根据口味称量。