Results 1 to 3 of 3
Thread: Help with random numbers
- 01-12-2009, 05:21 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
Help with random numbers
Hello,
I am trying to develop a virtual stock market in which I need to generate random numbers for changes in daily stock value. I will show you an example of a declaration:
public double getDowJonesChange()
{
return generator.nextDouble() * (getDowJonesCost()*0.08) + (getDowJonesCost()*(-0.08));
}
I am trying to allow the stock to go up or down 8% per day. Any help at all is appreciated. Also, in case you guys were wondering, I already declared Random generator = new Random() and also imported java.util.Random
Thanks Again!Last edited by checkmylongboarding; 01-12-2009 at 05:26 AM.
- 01-12-2009, 05:42 AM #2
The range is 16, so nextDouble() * 0.16; then - 0.08 .....
probably, I'm not a mathemetician.
Not exactly difficult, deceptively simple. Just work through it once and the approach tend to stay available to your coding.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-12-2009, 05:47 AM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Two options:
(1) the overall range of change is 0.08 + 0.08 = 0.16. So you need to multiply your nextDouble() by 0.16. This gives you a number in the range 0 to 0.16. Then subtract 0.08, to give a number in the range -0.08 to 0.08. A slight issue is that nextDouble() will never return the exact value 1, so your result will be very very minutely biased towards a negative change.
(2) separately call nextDouble() for the magnitude of change, then nextBoolean() to decide if it's upwards or downwards.Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Random Numbers from 1 onwards
By Ciwan in forum New To JavaReplies: 7Last Post: 12-18-2008, 02:31 PM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks