# Thread: rgen acm library stanford online course basic problem about passing and holding doubl

1. Member
Join Date
Mar 2011
Posts
6
Rep Power
0

## 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);

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 01:32 AM.

2. ## 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.

3. Member
Join Date
Mar 2011
Posts
6
Rep Power
0

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

Originally Posted by Norm
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.