# Thread: Help with random numbers

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.

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.

3. Senior Member
Join Date
Nov 2008
Posts
286
Rep Power
13
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.

#### Posting Permissions

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