Java random number generator help.

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 4K views
the other guy
Messages
19
Reaction score
0
Hi, I'm trying to get it so that when the program initiates, if the random number is greater than 3, I turn left. here is what I have in summary;

int rt=3;
Random ra = new Random();
for (int c = 6; c <12; ++c);
{
}
if(rt<ra)
{
Rbot.turnLeft();
}

the error says; operator < cannot be applied to int,java.util.Random
if this is the case, how can i get use the random number generator toward my end? I want the random number generator to be the source of "randomly occurring actions" for my robot.
 
Physics news on Phys.org
the other guy said:
Hi, I'm trying to get it so that when the program initiates, if the random number is greater than 3, I turn left. here is what I have in summary;
I added [ code] tags.
the other guy said:
Code:
int rt=3;
Random ra = new Random();
for (int c = 6; c <12; ++c);
{
}
if(rt<ra)
{
   Rbot.turnLeft();
}
the error says; operator < cannot be applied to int,java.util.Random
if this is the case, how can i get use the random number generator toward my end? I want the random number generator to be the source of "randomly occurring actions" for my robot.
There are several things wrong with your code.
1) ra is an instance of the Random class. It is not a random number. Look http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/util/Random.html for a summary of the methods that are exposed by this class.

The error you are getting says essentially that you cannot compare a number and a class instance.
2) Your for loop is confusing (to some). What are you trying to get it to do? As it is now, all it does is count from 6 to 12 - nothing more.
3) You have a pair of braces following the for statement. Why are they there? If you removed them it would make no difference in what your code produces.
 
Last edited by a moderator:
Try:
int x = ra.nextInt();
which should give you a value from 0-31, or
double x = ra.nextDouble();
which should be between 0.0 and 1.0

Also follow all the advice of the previous poster,
and pay some attention to Random.setSeed()