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.

Results 1 to 3 of 3

Thread: Generate random numbers between 1 and 52 by passing a seed to Random.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generate random numbers between 1 and 52 by passing a seed to Random.

    Hi,

    I have to generate random numbers to shuffle a deck of 52 cards. I am using below code:

     
    import java.util.Random;
     
    for (int i = 0; i < 52; i++) {
    long seed = System.currentTimeMillis();
    Random random = new Random(seed);
    int i = random.nextInt(52);
    }

    But I am getting duplicate random numbers as the seed having same value for the next iterations.

    Please help to fix this.

    Regards,
    Shareef


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Generate random numbers between 1 and 52 by passing a seed to Random.

    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?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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

Similar Threads

  1. How to use the Random statement to generate a random attack.
    By Shzylo in forum Java Theory & Questions
    Replies: 5
    Last Post: March 3rd, 2013, 07:05 PM
  2. Replies: 1
    Last Post: February 15th, 2013, 08:03 PM
  3. Game of Life 2-D matrix random seed boolean array PLEASE
    By Eriosblood in forum Collections and Generics
    Replies: 20
    Last Post: September 3rd, 2012, 06:10 PM
  4. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM