我有一个的java.util.List ,每提到一个图像1000个唯一对象,在1000列表中的每个图像都是独一无二的,现在我想洗牌他们,这样我可以使用的第一个20个对象和present他们的网站用户。然后,用户可以点击一个按钮,说:洗牌,我从头开始,并再次呼吁再次获得1000张洗牌()。然而,似乎出1000的图像对象,我经常一次又一次地看到20图像选择之间相同的图像。
I have 1000 unique objects in a java.util.List, each referring to an image, each image in the 1000-list is unique and now I'd like to shuffle them, so that I can use the first 20 objects and present them to the website-user. The user can then click a button saying "Shuffle", and I retrieve the 1000 images again from scratch and calling again shuffle(). However, it seems that out of 1000 image objects, I very often see the same image again and again between the 20-image-selections.
有些东西似乎是错的,什么更好的建议,意见?
Something seems to be wrong, any better suggestion, advices?
我的code是非常简单的:
My code is very simple:
List<String> imagePaths = get1000Images(); Collections.shuffle(imagePaths); int i = 0; for (String path: imagePaths) { ... do something with the path ... i++; if (i >= 20) break; }我知道 Collections.shuffle()分布均匀:例如参见blog.ryanrampersad/2012/03/03/more-on-shuffling-an-array-correctly/
不过,我只是有一种感觉,一组20张出1000的一遍一遍的看同一图像的概率要少得多...
However, I just have the feeling that the probability of seeing the same image over and over again in a set of 20 images out of 1000 should be much less...
输入AP高preciated。
Inputs highly appreciated.
推荐答案如果你正在展示20张出1000看到的概率中的任何一个20 的下一次迭代重复约0.34,所以你不应该感到惊讶地看到图像重复。
If you're showing 20 images out of 1000 the probability of seeing any one of that 20 repeated in the next iteration is approximately 0.34 so you shouldn't be surprised to see images repeating.
看到一个具体的形象的机会仍然是千分之一,但如果你正在寻找二十图像的机会要高得多。
The chances of seeing a specific image is still one in a thousand, but if you're looking for twenty images the chances are much higher.
我们可以计算出没有previous的概率20幅图像的重复:
We can calculate the probability of none of the previous 20 images repeating as:
980 979 961 ———— × ——— × ... × ——— ≈ 0.66 1000 999 981和所以看到重复的概率是一减此,或约0.34
And so the probability of seeing a repeat is one minus this, or approximately 0.34.
和看到无论是在接下来的两个迭代反复的图像的概率是:
And the probability of seeing an image repeated in either of the next two iterations is:
1 - (0.66 × 0.66) ≈ 0.56在换句话说,它比没有更可能的是,你会看到一个重复的图像在以下两个周期。 (这不包括从第三个第二个周期重复的图像这只会使其更加容易。)
In other words, it's more likely than not that you'll see a repeated image over the two following cycles. (And this isn't including images repeated from the second cycle in the third which will only make it more likely.)
有关它的价值,这里的一些Java code做上述计算:
For what it's worth, here's some Java code to do the above calculation:
float result = 1.0f; int totalImages = 1000; int displayedImages = 20; for (int i = 0; i < displayedImages; i++) { result = result * (totalImages - displayedImages - i) / (totalImages - i); } System.out.println(result);更多推荐
是Collections.shuffle()真的够乱?实际的例子似乎否认这一说法
发布评论