Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java random number generator help.

  1. Jul 8, 2010 #1
    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 occuring actions" for my robot.
     
  2. jcsd
  3. Jul 8, 2010 #2

    Mark44

    Staff: Mentor

    I added [ code] tags.
    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 [Broken] 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: May 4, 2017
  4. Jul 9, 2010 #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()
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java random number generator help.
Loading...