Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I need Rand.nextInt to run only once?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code, I need to calculate a random number between -1 and 1, but I have it in an if statement and I need it to ONLY calculate the random number every time the if statement is true, NOT WHILE it is true. However, it seems to be constantly recalculating the number here. Please help.


else{




if(!hasNearestPlant){

double randX = (rand.nextInt(3)-1)/1000.00;
double randY = (rand.nextInt(3)-1)/1000.00;

myX = myX + randX;
System.out.println(randX);
myY = myY + randY;
}


 
Bartender
Posts: 1460
31
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse, you're going to have to give us more code than that, if we're going to be able to help you. Also, please UseCodeTags.
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Jesse, you need to return to the earlier thread you started and let the volunteers know whether their responses were helpful, and whether you still have a problem.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!