Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Rep Power

    Default Help with random numbers

    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. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    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

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power


    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.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Random Numbers from 1 onwards
    By Ciwan in forum New To Java
    Replies: 7
    Last Post: 12-18-2008, 02:31 PM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

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