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

Thread: Java Math.random() and loop

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

    Default Java Math.random() and loop

    Could anyone tell me why this loop of mine only run once? So this would run if the user select option "2", the passLength is the length the user input in. I choose 3 as an input, won't let mean my while loop will be running 3 time? As you can see, I put a println there so it print everytime it run, but I only get 1 output.

         String LowerUp="";
         int randomLowUp = 91;
     
          if(UserInput.equals("2"))
         for(int LowerLAmount = 1; LowerLAmount <= passLength;LowerLAmount++)
         {
            while(randomLowUp > 90 && randomLowUp < 97)
            {randomLowUp = 0;
              randomLowUp = (int)((Math.random()*(57))+(65));
              LowerUp += (char)(randomLowUp); 
              System.out.println (LowerUp);
        }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Could anyone tell me why this loop of mine only run once?
    Add some println statements inside and at the bottom of the loop that prints out the values of the variable(s) that control the looping in the while statement. Seeing the values of the variables will help you understand what the code is doing.

    What are all the "magic" numbers for? 90, 97, 57 and 65
    If they are the values of chars then code them as chars: 'A' etc
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    It is doing exactly what it suppose to except the loop stop after 1. I can't seem to figure out why it would just stop after loop one. Then I put a println to print the "LowerLAmount" to see how many time the for loop is runing but it stop after 1.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    why it would just stop after loop one
    What is the value of the while loop's controlling variable that is printed out?
    What values in that variable will stop the looping?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    The while loop would print out a value from 65-122. If it encounter a value of 90 to 97, it would keep looping. Ahh.... I know where this is going. I guess because I put since the LowerLAmount can be less than or equal to passLength so when the while loop stop, the for loop also stop?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Did you add the println statement i suggested?
    What is the value of the while loop's controlling variable that is printed out?
    What values in that variable will stop the looping? Was the printed value going to allow the loop to continue?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    I'm kind of confused by the question, but the while loop will loop to try and print a number from 65-122. The loop will keep looping if it encounter a number from 90-97 so it the value in the variable that will stop the looping is any value from 65-90 and 97-122

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    I'm kind of confused by the question
    Can you explain what the confusion is? I suggested you add a println statement inside and at the end of the while statement that prints out the value of the variable the controls the while statements looping.
    for example:
      int loopCntrlVar = 0;
      while (loopCntrlVar < 2) {
        loopCntrlVar = 5;
        System.out.println("lpCV="+loopCntrlVar);  //  print value of loop control variable
      }  // end while loop

    Add the println, execute the code and copy the program's output and paste it here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    For my case won't the value of the loop control variable be any value from 65-122 excluding 90-97?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    What is printed out when you execute the code with the added println statement?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    It would be a different value each time but this time it "69"

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    this time it "69"
    What values does it need to be for the while loop to continue executing?
     while(randomLowUp > 90 && randomLowUp < 97)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    The value need to be between 90 and 97 for the loop to continue.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Is 69 between 90 and 97?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    No ,so that why it stop?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Yes, when the value is not in the range shown in the while statement, the loop will stop.

    What is the while loop supposed to do? Why have the while loop?
    You can write an expression using a random number generator that will generate numbers in any range you want.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    I'm trying to generate random letter of upper case and lower case from a-z. I can't use array so I figure to generate number from 65-122 excluding 90-97 because those are not letter.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Please don't use "magic" numbers like 90 and 97. I do not know what char they are supposed to be.
    Use a char value like 'A' or '9'.
    You are trying to get values from 'A' to 'z' skipping over the values between 'Z' and 'a'.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    I'll keep in mind about "magic" numbers for the next program , thank you.Yes, as you say, I'm trying to get values from 'A' to 'z' skipping over the values between 'Z' and 'a'. Now that you help me understand why it stop running, I realize my for loop stop because the while loop terminate. Is there anything I could do to make the for loop continue for whatever the value the user might input for "passLength"? Thanks
         String LowerUp="";
         int randomLowUp = 91;
     
          if(UserInput.equals("2"))
         for(int LowerLAmount = 1; LowerLAmount <= passLength;LowerLAmount++)
         {
            while(randomLowUp > 90 && randomLowUp < 97)
            {randomLowUp = 0;
              randomLowUp = (int)((Math.random()*(57))+(65));
              LowerUp += (char)(randomLowUp); 
              System.out.println ("LowerUp:"+LowerUp);
              System.out.println("randomLowUp:"+randomLowUp);

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    Sorry, I can't help you with code that has so many magic numbers. I don't want to have to look them up every time I see them.

    Can you explain what the steps the code should take to solve your problem? The posted code doesn't have any comments explaining what it is trying to do in each group of statements.

    For example: what is the purpose of the while loop?
    What does the code need to do to enter the while loop?
    What needs to happen for the execution to leave the while loop?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    Ok thank you. I'm guessing magic number mean using number in source code without explanation? How exactly can I fix " magic" number?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    How exactly can I fix " magic" number?
    Replace the number with the char. Use 'A' and 'z' instead of the numbers the code is using
    See curmudgeon's post#4 in this thread:
    http://www.javaprogrammingforums.com...html#post88270
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Math.random() and loop

    Ok, I will fix all the magic number. Is it possible for you to give me a hints of how to generate both lower and upper case letter without array and .charAt?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Math.random() and loop

    You need to continue debugging your code to see why it is doing what it does.
    Add a println statement just before the while statement that prints out the value of the while statement's loop control variable. The same one you are already printing at the end of the while loop.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (December 28th, 2012)

Similar Threads

  1. Math.random() with no duplictaes
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 07:42 AM
  2. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 06:18 PM
  3. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  4. Need help understanding math.random method.
    By slashdash in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2012, 05:50 PM
  5. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM