Results 1 to 3 of 3
  1. #1
    elfdreaming is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default rgen acm library stanford online course basic problem about passing and holding doubl

    Hi

    What I want to do is create a randomly generated number (using acm libraries) and then go on to use that exact number for several loops.

    (what I am trying to do is randomly generate an increment, and then move the ball at that increment each time).

    What I am doing, however, is generating a new random number each time.

    How do I create a variable which takes the double but does not simply point to where I created the rgen?

    I simplified version of the code is as follows:
    (no, this code will not run as is, it is simplified down to the problem).

    Java Code:
    public void run(){
    
    ball = new GOval(width, height);
    ball.setFilled(true);
    add(ball, x, y);
    
    while (true){
    double xDir = xMovement();
    //println("xDir" + xDir);
    double blahX = xDir;
    //println("blahX " + blahX);
    ball.move(blahX, blahY);
    }
    private double xMovement(){
    return rgen.nextDouble(-3.0, 3.0);
    }
    
    
    RandomGenerator rgen = new RandomGenerator();
    }
    It does not seem to matter whether the rgen is in a separte method or not.

    Sorry if this is a stupid question. I went away for quite a while and I came back and maybe I have forgotten some basic elements.
    Last edited by elfdreaming; 01-18-2013 at 02:32 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: rgen acm library stanford online course basic problem about passing and holding d

    What I am doing, however, is generating a new random number each time.
    Calling a method inside a loop means it will be called every time the code goes around the loop.
    If the call to the method is outside the loop it would not be called every time.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    elfdreaming is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Re: rgen acm library stanford online course basic problem about passing and holding d

    Quote Originally Posted by Norm View Post
    Calling a method inside a loop means it will be called every time the code goes around the loop.
    If the call to the method is outside the loop it would not be called every time.
    Ah yes THANKYOU!

    I was looking at the problem all wrong. This makes perfect sense and more importantly, works.

Similar Threads

  1. Replies: 9
    Last Post: 11-16-2012, 03:44 PM
  2. Holding mouse
    By d2r123 in forum New To Java
    Replies: 10
    Last Post: 03-13-2012, 10:55 PM
  3. Replies: 1
    Last Post: 03-08-2011, 10:11 PM
  4. Basic Q: passing to drawRect() in an applet
    By hayden06f4i in forum New To Java
    Replies: 20
    Last Post: 12-08-2010, 04:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •