Random number to 4 DP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
megaming
Forum Commoner
Posts: 27
Joined: Wed Dec 25, 2002 12:31 pm

Random number to 4 DP

Post by megaming »

Does anyone know how to get a random number with a lower bound of 0.0001 and an upper bound of 100.0000 ?

This would help me greatly, thank you :D
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Dunno if this results in what you desire, but a good start perhaps:

Code: Select all

<pre>
<?php 
$upperBound = 100.0000;
$lowerBound = 0.0001;
$range = $upperBound-$lowerBound; 
$num = $lowerBound + $range * mt_rand(0, 32767)/32767;
echo round($num,4);
?>
megaming
Forum Commoner
Posts: 27
Joined: Wed Dec 25, 2002 12:31 pm

Post by megaming »

Hmm... looks like it could work. I don't need the code for a couple of days, I'm just asking so I don't have to wait when I do need it :)

If anyone else has alternative methods, then that could still help incase this one doesn't work.

Thank you :D
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

megaming wrote:I'm just asking so I don't have to wait when I do need it :)
Hehe, good call.
Post Reply