Hi,
I have to generate random numbers to shuffle a deck of 52 cards. I am using below code:
But I am getting duplicate random numbers as the seed having same value for the next iterations.
Please help to fix this.
Regards,
Shareef
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
I have to generate random numbers to shuffle a deck of 52 cards. I am using below code:
But I am getting duplicate random numbers as the seed having same value for the next iterations.
Please help to fix this.
Regards,
Shareef
Erm, it's called random. You're never safe not to get duplicates. Maybe you could fill an int array with numbers from 1 - 52 and shuffle it?
A seed is used too set a pseudo random generator to a specific state.
So if you create two Random objects with the same seed it will repeat the same random sequence.
This usefull if you need something to be repeatable, but i am guessing that is not what you want.
Try reading the javadoc for the seed constructor of Random again
Hint: only create one seeded Random object and reuse it.
Rolf