Lost Password?


  #1 (permalink)  
Old 08-26-2007, 03:09 PM
Paradine Paradine is offline
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 8
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
Forum Jump

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


All times are GMT -5. The time now is 08:57 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads