Can a Collections.shuffle be considered equivalent

2019-07-08 20:19发布

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?

标签: java random
1条回答
时光不老,我们不散
2楼-- · 2019-07-08 20:55

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.

查看更多
登录 后发表回答