note 74793 deleted from function.rand by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: DKDiveDude at Subima dot com 

----

I was kind of surprised that PHP did not have a built-in floating point random number function. So I created my own:

<?
// Floating point random number function
// April 2007, René W. Feuerlein
function fprand($intMin,$intMax,$intDecimals) {
  if($intDecimals) {
    $intPowerTen=pow(10,$intDecimals);
    return rand($intMin,$intMax*$intPowerTen)/$intPowerTen;
  }
  else
    return rand($intMin,$intMax);
}

// 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>';
}
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.