Results 1 to 3 of 3
- 01-18-2013, 01:25 AM #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).
It does not seem to matter whether the rgen is in a separte method or not.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(); }
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.
- 01-18-2013, 02:31 AM #2
Re: rgen acm library stanford online course basic problem about passing and holding d
Calling a method inside a loop means it will be called every time the code goes around the loop.What I am doing, however, is generating a new random number each time.
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.
- 01-18-2013, 02:46 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Help Please, Learning To Program In Java Through Stanford Free Online Course
By Powermaniac in forum New To JavaReplies: 9Last Post: 11-16-2012, 02:44 PM -
Holding mouse
By d2r123 in forum New To JavaReplies: 10Last Post: 03-13-2012, 09:55 PM -
Yahtzee: yahtzeelib.jar not recognized (Stanford CS106A Assignment5 online)
By eXistenZ in forum New To JavaReplies: 1Last Post: 03-08-2011, 09:11 PM -
Basic Q: passing to drawRect() in an applet
By hayden06f4i in forum New To JavaReplies: 20Last Post: 12-08-2010, 03:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks