gpt4 book ai didi

javascript - 仅向进入我网站的 20% 的人显示 div

转载 作者:行者123 更新时间:2023-11-28 17:48:07 25 4
gpt4 key购买 nike

我想只显示我网站 20% 流量的 div,所以我做了这样的逻辑:

var div = (Math.floor(Math.random() * 100) + 1) > 20 ? true : false;
if (div)
// don't show
else
// show

但我认为这并不准确,是吗?

谢谢。

最佳答案

考虑到所有的随机性,需要有足够大的样本量才能显示正态分布。正如您所看到的,您获得的访问者越多,该函数就越接近 20%

function visitors(n) {
var visited = 0;
for (var i = 0; i < n; i++)
visited += (Math.floor(Math.random() * 100) + 1) > 20 ? 0 : 1;
console.log("Out of " + n + " visitors, " + visited + " were shown the div. (" + visited / n * 100 + "%)");
}

[1, 10, 100, 1000, 10000, 100000, 1000000].forEach(x => visitors(x));

Radmation但是,提到过,对于较少数量的访问者,使用服务器将使您更接近 20%

I wouldnt use client side logic for this but server side logic. You could literally increment a server side variable and every time it gets to 5 reset it and tell the client to show the div. Of course start with 1 in this case. If you need to be exact at least -> you could use ajax as well to get the counters value - if you run into multiple requests at once.

关于javascript - 仅向进入我网站的 20% 的人显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com