-
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.
Code :
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);
}
}
-
Re: Java Math.random() and loop
Quote:
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
-
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.
-
Re: Java Math.random() and loop
Quote:
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?
-
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?
-
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?
-
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
-
Re: Java Math.random() and loop
Quote:
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:
Code :
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.
-
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?
-
Re: Java Math.random() and loop
What is printed out when you execute the code with the added println statement?
-
Re: Java Math.random() and loop
It would be a different value each time but this time it "69"
-
Re: Java Math.random() and loop
What values does it need to be for the while loop to continue executing?
Code :
while(randomLowUp > 90 && randomLowUp < 97)
-
Re: Java Math.random() and loop
The value need to be between 90 and 97 for the loop to continue.
-
Re: Java Math.random() and loop
-
Re: Java Math.random() and loop
-
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.
-
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.
-
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'.
-
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
Code :
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);
-
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?
-
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?
-
Re: Java Math.random() and loop
Quote:
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
-
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?
-
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.