# Help with random numbers

• 01-12-2009, 06:21 AM
checkmylongboarding
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!
• 01-12-2009, 06:42 AM
Nicholas Jordan
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.
• 01-12-2009, 06:47 AM
neilcoffey
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.