1

我在为我的问题考虑算法时遇到了一些麻烦。我会尽力解释。这只是一个例子。

假设 1000 人中有 3% 的人点击了链接。

所以我需要一种算法,可以将点击随机传播到 1000 多个视图。

很容易将其均匀分布,例如:1000 * .03 = 30,因此每 30 次浏览就会有人点击一个链接。

例如:

For (i = 0; i<1000; i++) {
if(i % 30 == 0);
click()
}
4

3 回答 3

4

您实际上需要 30 次点击,还是只需要平均每 1000 次点击 30 次的统计分布?如果您只需要统计分布,那么只需执行if (Math.random() < 0.03). 如果您恰好需要 30 次点击,那么最简单的方法是选择 0 到 999 之间的 30 个随机数,确保没有重复项,然后单击这些索引。

于 2013-03-26T23:39:49.540 回答
2

You could also create an array of 1000 integers, initialize 30 of them to 1 and the rest 970 to 0, then shuffle the array randomly and use array[index from 0 to 999] to see if a click is needed.

于 2013-03-26T23:38:52.287 回答
1
List<Boolean> wasclicked = new ArrayList<Boolean>();
for (int i = 0; i < NUM_CLICKS * 0.03; i++) {
    wasclicked.add(true);
}
for (int i = 0; i < NUM_CLICKS * 0.97; i++) {
    wasclicked.add(false)
}
Collections.shuffle(wasclicked);

您现在有一个随机的布尔值列表,其中 3% 为真。

于 2013-03-26T23:42:56.757 回答