Java random number generator help.

Click For Summary
SUMMARY

The forum discussion centers on the implementation of a random number generator in Java to control a robot's movement. The user attempts to compare an integer with an instance of the Random class, leading to a compilation error. Key corrections include using methods like ra.nextInt() to generate a random number and clarifying the purpose of the for loop. Additionally, the discussion highlights the importance of understanding the Random class methods and the use of Random.setSeed() for reproducibility.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of the java.util.Random class
  • Basic control structures in Java (loops and conditionals)
  • Knowledge of method invocation in Java
NEXT STEPS
  • Learn how to use Random.nextInt(int bound) for bounded random number generation
  • Explore the Random.setSeed(long seed) method for generating reproducible sequences
  • Investigate the use of random numbers in game development for AI behavior
  • Study Java control flow statements to improve code logic and structure
USEFUL FOR

Java developers, robotics programmers, and anyone interested in implementing random behavior in software applications.

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()
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
22
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
2K
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