Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate a Random number between x and y

Status
Not open for further replies.

RBCDS

Technical User
Apr 6, 2001
24
0
0
CA
Does anyone know how to generate a random number that falls between a given range???

Dan
 
Just generate your random number, multiply it by the range, subtract the lower limit :)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thanks, I really needed that for a sim I was working on.

public double generate(int min, int max)
{
double rdmNo = Math.random() * (max - min) + min;
return rdmNo;
}

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top