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 2 of 2

Thread: Java random number

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java random number

    So I need to generate 5 number and in this 5 number, one of the number must match the number I'm looking for. Let say the number is "1". I generate one number at a time but there must be a "1" by or before the 5th number. The problem with my code is that it continue to loop until "1" is found. Thanks

           for (int counter = 1; counter <= 2; counter++) 
           {  
             int randNum1 = 0;  
             int bottleCapNum = 0;
             while (randNum1 != 1)
             {  
                randNum1 = randNum.nextInt(5);  
                bottleCapNum++;  
             }  
             wins++;  
             outFile.println(bottleCapNum);        
           }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,121
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Java random number

    there must be a "1" by or before the 5th number
    You can not guarantee what the next random value will be. It's possible for there to be many repeats before the desired value is returned.
    Another way would be to use the shuffle method with a series of numbers then the order of the numbers would be random.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Random number
    By Imran Ahmad in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2012, 11:53 AM
  2. Generate random number in Java
    By waiheng1986 in forum Java Programming Tutorials
    Replies: 1
    Last Post: March 11th, 2012, 12:55 PM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  4. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  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