Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Can a for loop work with random number?

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Can a for loop work with random number?

    Hi all,

    I need to write code to make a kangaroo object jump a random number of times between 1 & 6 times. I have an instance variable declared of type random, called ranNumber. I also have a getter method for this variable. Just so you know the message jump takes no argument. I think i need to use a for loop for this so the code I have come up with so far is:

     public void kangarooJump(Kangaroo aKangaroo)
         {
            ranNumber.nextInt(7);
            for (int jumps = 0; jumps < getRanNumber(); jump ++)
            {
               aKangaroo.jump();
            }
     
         }

    The problems I am having are when i try to compile I get an error message "<operator cannot be applied to int, java.util.Random. I understand why this error is coming up but I cant tink of a way to resolve it.

    Is it even possible to do what I want with a for loop or is there another way to do it?
    it needs to be kept as simple as possible as im only in the early stages of learning Java.

    Hope someone can help. Thankyou in advance to anyone who takes the time to read this.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can a for loop work with random number?

    You should draw the random prior to initiating the loop. I don't know what getRanNumber() actually does because the code isn't posted for it, but based upon this it doesn't look like its needed

            int max  = ranNumber.nextInt(6) + 1;//add one because you want to jump between 1 and 6
            for (int jumps = 1; jumps < max; jump ++)//start loop at 1 and go for max times
            {
               aKangaroo.jump();
            }

  3. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (January 19th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can a for loop work with random number?

    use Math.random.... thi is the class that usually use to generate random numbers

  5. The Following User Says Thank You to chronoz13 For This Useful Post:

    humdinger (January 19th, 2010)

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can a for loop work with random number?

    There are two ways to generate random numbers:
    Math.random (eh, basic but functional) and the Random class. Math.random() generates a double on the interval [0,1) (includes 0, doesn't include 1).

    Random can be used to generate a variety of different data types right away.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    humdinger (January 19th, 2010)

  8. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can a for loop work with random number?

    As a side note: behind the scenes, Math.random uses a static instance of Random and its nextDouble function to generate a random number. IMO that 'middle man' design makes getting a random number much easier because you can do so from a static context.

  9. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (January 19th, 2010)

  10. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can a for loop work with random number?

    so whats the better between the two?
    confusing... because i got used to the .random() of Math.. do they have similar purpose?

  11. The Following User Says Thank You to chronoz13 For This Useful Post:

    humdinger (January 19th, 2010)

  12. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can a for loop work with random number?

    Quote Originally Posted by chronoz13 View Post
    so whats the better between the two?
    I don't think there is a right or wrong answer to this question...it depends upon context. Math.random is static, and so can be easily accessed across classes and this alone may make it more often used. Random must be instantiated, but provides easy methods to retrieve primitive types. One major advantage to Random is that it can be extended - say for example to create methods to return random objects in addition to primitives - making it advantageous in that sense.

  13. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (January 19th, 2010)

  14. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can a for loop work with random number?

     public void kangarooJump(Kangaroo aKangaroo)
         {
            ranNumber.nextInt(7);
            for (int jumps = 0; jumps < getRanNumber(); jump ++)
            {
               aKangaroo.jump();
            }
     
         }

    If you're doing Math.random(), then one of your problems is that you're trying to set a for loop to run for a fraction of a time. You might be setting it to
     for(int jumps = 0; jumps < 0.1; jumps++)
    You can't have a loop do a fraction of an iteration. You should probably rather use
     Random randGenerator = new Random(); 
    int number = randGenerator(7);
    for(int jumps = 0; jumps<number; jumps++)

    That should fix your problem. Hope that helped!

  15. The Following User Says Thank You to cmh0114 For This Useful Post:

    humdinger (January 19th, 2010)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. my program. Random number generater.
    By james137 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2009, 02:58 PM
  3. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM
  4. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM