Why the Random pre-defined functions create an error in the code?
Hi all,
I have a problem with my code when I add this line:
inject=aRandom.nextBoolean();
I have a loop more than what it is supposed to be. Iterations should stop at 10 but it continues until it reaches 13 or 15.
Here is my code:
Code java:
import java.util.Random;
public class Test1_FT1 {
static boolean inject=true;
static boolean previnj=true;
static Random aRandom = new Random();
public static boolean check(int v1, int v2){
inject=aRandom.nextBoolean();
if(v1<v2){
if(inject){
return false;
}
else
return true;
}
if(inject)
return true;
return false;
}
static int nmax=10;
public Test1_FT1() {
int i=0;
while((check(i,nmax))!=(check(i,nmax)));
while((check(i,nmax))) {
System.out.print("nmax: ");
System.out.print(nmax+"\n");
i++;
}
}
public static void main(String[] args) {
new Test1_FT1();
}
}
Re: Why the Random pre-defined functions create an error in the code?
Quote:
Iterations should stop at 10 but it continues until it reaches 13 or 15.
The posted code does not compile. Make sure you post the same version of the code you are having problems with.
Re: Why the Random pre-defined functions create an error in the code?
Quote:
Originally Posted by
jps
The posted code does not compile. Make sure you post the same version of the code you are having problems with.
OOOOPS .. Am so sorry, I just edited the code. Thank You.
Re: Why the Random pre-defined functions create an error in the code?
What is the code supposed to do?
What does the first while loop do?
Quote:
while((check(i,nmax))!=(check(i,nmax)));
Re: Why the Random pre-defined functions create an error in the code?
Quote:
Originally Posted by
jps
What is the code supposed to do?
What does the first while loop do?
This is only to make sure that no Error occurs. Kind of fault avoidance that's why I compute it twice.
Re: Why the Random pre-defined functions create an error in the code?
Quote:
Originally Posted by
Sharmeen
This is only to make sure that no Error occurs. Kind of fault avoidance that's why I compute it twice.
I think you do not understand what happens then. Consider the first loop:
while((check(i,nmax))!=(check(i,nmax)));
So while (this) does not equal (that) where 'this' and 'that' are calls to the same method with the same parameters (0 and 10)
Now inside the method, inject=aRandom.nextBoolean(); places a boolean value in inject
if(v1<v2){ Well 0 is less than 10 every time so this is always true.
if(inject){ inject holds the random boolean value assigned above and in theory has a 50/50 chance of being heads or tails.
So what happens is the method now returns either true or false depending on the value of the random boolean.
Pretend this time it returns true. Now the whole process starts over again at calling the method for the second time in the first while loop.
The steps take place with 0 and 10 again, and this time if the boolean generated is false, the while loop ends. If the method returns true this time, the while loop goes to it's body, which is just a semicolon so it does nothing, and the iteration starts over again. This loop continues to run until the random boolean returns a true and a false value on the same iteration. When that finally happens (silently and quickly behind the scenes) everything else continues as if nothing happened. There are no side effects of the line of code other than wasted processor time.
If you do not follow what I am saying I suggest you do some reading about while loops in java.
Re: Why the Random pre-defined functions create an error in the code?
Thank you very much for the reply. But even if I took off that line, it doesn't print correctly. Any other suggestions??
Re: Why the Random pre-defined functions create an error in the code?
Well you still have not told me what you expect the code to do, so I can't say much about what might not be cooperating...