Floating Point random number generator

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
This code generates a random floating point (decimal) number:

PHP:
<?
// Floating point random number function
function fprand($intMin,$intMax,$intDecimals) {
  if($intDecimals) {
    $intPowerTen=pow(10,$intDecimals);
    return rand($intMin,$intMax*$intPowerTen)/$intPowerTen;
  }
  else
    return rand($intMin,$intMax);
}

Usage:

PHP:
// Example of fprand function
for($i=0; $i<=5; $i++) {
  echo "Three random numbers with $i decimals are: ";
  for($ii=1; $ii<=3; $ii++)
    echo fprand(1,10,$i).' ';
  echo '<br>';
}
?>


</span></span>

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top