开发者

Can a Collections.shuffle be considered equivalent to a series of Randoms?

Following this question: Shuffling numbers in a map and selecting one.开发者_如何学C

Say you need to select a series of integers randomly from a List. Could a Collections.shuffle be considered as equivalent to repeatedly using Random#nextInt?

I'm not familiar with how shuffle is implemented and whether they could be seen as truly equivalent from a mathematical point of view (permutations). The link below does insist on the importance of using a single Random object.

http://www.javapractices.com/topic/TopicAction.do?Id=62

P.S: I'm aware that Collections.shuffle adds an operation in that it actually reorganizes content. It's the result I'm interested in.

Edit: Found this question on SO detailing the shuffle method as using what's called a Fisher-Yates shuffle: Java's Collections.shuffle is doing what?


If you're asking if using Collections.shuffle and then using the resulting list of randomly-ordered numbers is equivalent to picking them one by one by using Random, the answer is no. The latter will quite likely return you the same index twice, which will result in duplicates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜