This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections.shuffle is displaying duplicates  RSS feed

 
Ganapathy Asha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to shuffle the numbers in the list.
For this created


My question is when i am sending no duplicates, why is colellections.shuffle adding duplicates.
Please suggest.

Thanks,
Asha
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asha Gan apathy wrote:My question is when i am sending no duplicates, why is colellections.shuffle adding duplicates.

Answer: It isn't.

Could you show us the output of your program and what makes you think this is happening?

The only thing I could think of is that
  Intial list size
and
  After shuffle list size
display different values.

If you can show us a case where that's happening, along with the input, we'd have a lot more to go on.

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Asha Gan apathy wrote:My question is when i am sending no duplicates, why is colellections.shuffle adding duplicates.

Answer: It isn't.

Perhaps I should qualify that: Collections.shuffle() merely re-arranges the contents of a List, it doesn't change them; so if your List contained duplicates before, it will contain duplicates after.

I wonder if, perhaps, you were expecting shuffle() to remove duplicates? Because it won't.

HIH

Winston
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at line 32 and you will understand why a duplicate is added ;)
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misunderstand the concept of "Randomness" -- a random number in a series is not necessarily unique. If I were generating numbers between 1 and 9 randomly, it would be perfectly reasonable to get something like this: 1, 4, 3, 4, 7, 5, 8, 2, 1, 6, 5, 2, ... -- that is a sequence of digits that I just randomly typed out on my keyboard. The numbers, however, are not unique. If you're still not convinced, then how about trying to generate a very long sequence of random binary digits, where your only choices are 1 or 0 and you're not allowed to repeat any of the digits. You can't. The longest series you can get will have a length of two.
 
Ganapathy Asha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in this case i have the list elements added myself its not randomly generated.
My objective of this program is shuffle the list and then pick a random number to replace that with 0
Please help in understanding the 2nd output.


Please check the output:
----------------First Time--------------
Intial list size7
1
2
3
4
5
6
7
After shuffle list size 7
4
5
7
1
2
6
3
4
After replace list size 7
4
5
7
1
2
6
0
------Second time-----------------------------------------------
------There is duplicate after shuffle-------------------------
-----Suprisingly the last print of list looks very different----

Intial list size7
1
2
3
4
5
6
7
After shuffle list size 7
1
3
4
7
6
5
2
3
After replace list size 7
1
3
4
7
6
0
2

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using getValue() to print the number? Why didn't you give that class its own toString method? And as for random numbers. I once won enough money for beer for the whole weekend by betting heads repeatedly in a heads or tails game.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to do in Line 33 here?

The intent of your code is getting lost in the jumble and confusion of all these details.

EDIT - ok, I see that I should have read your last response in more detail.

Why are you surprised? It seems that the program did exactly what you told it to do. What were you expecting instead?
 
Ganapathy Asha
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops! i have printed the random number generated and thought that was part of the shuffled list.

Thanks a lot everyone
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it now. Go back to Winston's Piet's response. That's your clue.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!