Java random number generator help.

In summary, the code provided has several issues. The first is that the variable "ra" is an instance of the Random class, not a random number. To generate a random number, you can use the methods "nextInt()" or "nextDouble()". Additionally, the for loop is not performing any useful function and the extra braces following it serve no purpose. Finally, to ensure truly random numbers, consider using the "setSeed()" method.
  • #1
the other guy
19
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
  • #2
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:
  • #3
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()
 

1. How do I generate random numbers in Java?

To generate random numbers in Java, you can use the Random class from the java.util package. Create an instance of the Random class and then use the methods provided, such as nextInt() or nextDouble(), to generate random numbers of the desired type.

2. Can I generate a specific range of random numbers?

Yes, you can generate a specific range of random numbers by using the nextInt(int bound) or nextDouble() methods and specifying the upper bound or range of numbers you want to generate. For example, to generate a random integer between 1 and 10, you would use nextInt(10) + 1.

3. How can I ensure that my random numbers are truly random?

The Random class in Java uses a pseudorandom number generator (PRNG) to generate random numbers. While this is generally sufficient for most use cases, if you need a higher level of randomness, you can use the SecureRandom class from the java.security package. This class uses a more secure and unpredictable algorithm to generate random numbers.

4. Can I set a seed for the random number generator in Java?

Yes, you can set a seed for the random number generator by using the setSeed(long seed) method of the Random or SecureRandom class. This allows you to get the same sequence of random numbers each time you run your program, as long as you use the same seed value.

5. How can I generate random numbers in a specific distribution?

The Random class in Java generates random numbers in a uniform distribution, meaning each number has an equal chance of being selected. If you want to generate random numbers in a different distribution, such as a normal or exponential distribution, you can use the classes from the org.apache.commons.math3.random package, which provide various methods for generating random numbers in different distributions.

Similar threads

  • Programming and Computer Science
Replies
1
Views
628
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
641
  • Programming and Computer Science
Replies
4
Views
946
Back
Top