note 115363 added to function.mt-rand
| From: | falkartis at gmail dot com | Date: | Sat, 12 Jul 2014 15:34:11 +0000 |
| Subject: | note 115363 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199917@lists.php.net to get a copy of this message | ||
I wrote another function to get a random float, if its not precise enougth jut add some
'0' to the $mul parameter.
<?php
function f_rand($min=0,$max=1,$mul=1000000){
if ($min>$max) return false;
return mt_rand($min*$mul,$max*$mul)/$mul;
}
?>
I made following tests:
<?php
echo f_rand()."<br>"; //0.497153
echo f_rand(0.5)."<br>"; //0.857822
echo f_rand(0.5,0.6)."<br>"; //0.599956
echo f_rand(0,10)."<br>"; //5.801949
echo f_rand(0,2,2)."<br>"; //1.5
echo f_rand(0,2,10)."<br>"; //1.7
?>
----
Server IP: 72.52.91.14
Probable Submitter: 85.59.44.47
----
Manual Page -- http://php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/115363
Del: integrated -- https://master.php.net/note/delete/115363/integrated
Del: useless -- https://master.php.net/note/delete/115363/useless
Del: bad code -- https://master.php.net/note/delete/115363/bad+code
Del: spam -- https://master.php.net/note/delete/115363/spam
Del: non-english -- https://master.php.net/note/delete/115363/non-english
Del: in docs -- https://master.php.net/note/delete/115363/in+docs
Del: other reasons-- https://master.php.net/note/delete/115363
Reject -- https://master.php.net/note/reject/115363
Search -- https://master.php.net/manage/user-notes.php