set the double value using Random class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazzer
    New Member
    • Mar 2008
    • 6

    set the double value using Random class

    Hi,

    I need help. I just wondering whether i can set the double value using random class..i even use if statement to set it..but nothing..i wanna set between 0.5 n 0.7 value..

    double planetMass = 0.0;
    double sunMass = 0.0;
    Random rdm = new Random();
    double rand = rdm.nextDouble( );

    if (rand >= 0.5 && rand <= 0.7){
    likeliHood = (rand * planetMass) / sunMass;
    }else{
    rand = rdm.nextDouble( );
    }

    please helpp...

    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by blazzer
    Hi,

    I need help. I just wondering whether i can set the double value using random class..i even use if statement to set it..but nothing..i wanna set between 0.5 n 0.7 value..

    double planetMass = 0.0;
    double sunMass = 0.0;
    Random rdm = new Random();
    double rand = rdm.nextDouble( );

    if (rand >= 0.5 && rand <= 0.7){
    likeliHood = (rand * planetMass) / sunMass;
    }else{
    rand = rdm.nextDouble( );
    }

    please helpp...

    Thanks
    This is the canonical trick: the nextDouble() method returns a number in the
    interval R= [0,1). You can compute a number in the range [L,H) as follows:

    r in R so (H-L)*r+L in [L,H)

    So you have to do this 0.2*r+0.5 to get a uniformly distributed number in the
    range [0.5,0.7)

    kind regards,

    Jos

    Comment

    • blazzer
      New Member
      • Mar 2008
      • 6

      #3
      Thx A LOT!! It workss!!

      Comment

      Working...