Lost Password?

  #1 (permalink)  
Old 08-26-2007, 02:09 PM
Paradine Paradine is online now
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 7
Paradine is on a distinguished road
Default Floating Point random number generator

This code generates a random floating point (decimal) number:

PHP Code:
<?
// 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 Code:
// 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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Generator of Unequal Numbers Paradine PHP Tutorials 0 08-26-2007 02:11 PM
How do I program a random generator? eddiewillers General Programming 4 07-24-2007 02:26 PM
large floating point calculations zeos386sx General Programming 5 04-02-2007 02:31 AM
Number Cloning Ronin Java Help 3 12-21-2006 07:23 AM
I need to generate a random number in PHP dirkfirst PHP Forum 7 07-05-2006 09:58 PM


All times are GMT -5. The time now is 11:30 AM.

Contest Stats

satrian ........
jackluxford ........
gamiR ........
Fred@eisfi ........
gamers2000 ........
chili5 ........
TcM ........
z-e-o ........
CharlesHutchsky ........
Jude ........

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads