Java Java random number generator help.

Click For Summary
The discussion focuses on correcting a Java code snippet intended to use a random number generator to control the movement of a robot. The main issue arises from attempting to compare an integer with an instance of the Random class, which leads to a compilation error. The correct approach involves using methods from the Random class to generate actual random numbers. Suggestions include using `ra.nextInt()` to obtain a random integer or `ra.nextDouble()` for a random double value. Additionally, the for loop in the code is deemed unnecessary as it currently serves no functional purpose. Removing extraneous braces after the for loop is also recommended for clarity. The discussion emphasizes the importance of understanding how to properly utilize the Random class to achieve the desired random actions for the robot.
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.
 
Technology 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()
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
22
Views
5K
Replies
3
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
11
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
5K
Replies
19
Views
2K
Replies
3
Views
2K