【发布时间】:2019-09-14 08:18:47
【问题描述】:
我设计了适合偏离中心的钟形曲线的值。试运行似乎给出了理想的结果,但我有什么遗漏吗?
我的发现
我发现了一个认为会有所帮助的问题,但他们使用的是标准差。
JavaScript Math.random Normal distribution (Gaussian bell curve)?
从上面的问题:
-
问:
Math.random已经是正态分布了吗? - 答: 没有
我有什么
const ranks = [
['X' , 0.01, 0.01],
['SSS' , 0.03, 0.04],
['SS' , 0.06, 0.10],
['S' , 0.08, 0.18],
['A' , 0.11, 0.29],
['B' , 0.14, 0.43],
['C' , 0.21, 0.64],
['D' , 0.17, 0.81],
['E' , 0.12, 0.93],
['F' , 0.07, 1.00]
];
log('Total:', ranks.reduce((a, b) => a + b[1], 0).toFixed(2));
for (let i = 0; i < 10; i++) {
generate();
}
/* Public */
function generate() {
let percent = Math.random();
log(percent.toFixed(4), getRank(percent));
}
/* Protected */
function getRank(percent) {
return ranks.filter((rank, index) => {
let min = index > 0 ? ranks[index - 1][2] : -1;
return percent > min && percent <= rank[2];
})[0][0];
}
/* Private */
function log() {
let el = document.createElement('DIV');
el.innerHTML = [].slice.call(arguments).join(' ');
el.className = 'log-line';
document.body.appendChild(el);
}
body {
background: #666;
}
.log-line {
font-family: monospace;
background: #AAA;
margin: 0.5em 0.25em;
padding: 0.25em;
}
.log-line::before {
content: '>>> ';
}
【问题讨论】:
-
你想达到什么目的?在给定具有用户指定参数(例如,均值和标准差)的自定义分布的情况下,您想找到每个排名使用的权重吗?
-
@PeterO.我想生成一个可以在这些范围内的随机数。在这种情况下,平均值是 C。我可以把它拟合成不对称的钟形曲线吗? Like the green line in the 2nd figure.
标签: javascript random probability normal-distribution