Can a Collections.shuffle be considered equivalent

2019-07-08 20:17发布

问题:

Following this question: Shuffling numbers in a map and selecting one.

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?

回答1:

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.



标签: java random