我想将 33.3% 的访问者重定向到我网站的路径“/versionA”,将 33.3% 的访问者重定向到“/versionB”,将 33.3% 的访问者重定向到“/versionC”。
目前我只有 50:50 的份额,代码如下:
if(Math.random() > 0.5) {
window.location.href = "/versionA";
} else {
window.location.href = "/versionB";
}
我如何根据均等分布对 3 个重定向进行随机拆分?
最佳答案
像这样的东西应该工作得很好。
var rand = Math.random();
if (rand < 1/3) {
window.location.href = "/versionA";
} else if (rand > 2/3) {
window.location.href = "/versionB";
} else {
window.location.href = "/versionC";
}
关于javascript - 通过 JS 按百分比重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52042771/