|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-30 17:41 UTC] + at ni-po dot com
Description:
------------
I want to propose an example for mt_getrandmax:
Code:
// calculate random floating point number
function randomFloat($from = 0, $to = 1) {
return $from + mt_rand() / mt_getrandmax() * $to;
}
echo randomFloat(0, 1);
Sample Output:
0.32345942003814
As generating uniform random values in the mathematical sense is the only application I see for mt_getrandmax I think this should be included.
Patchesmt_getrandmax-add-example (last revision 2010-11-05 19:51 UTC by frozenfire at thefrozenfire dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 14:00:02 2025 UTC |
Ooops, I'm sorry, the code should be: // calculate random floating point number function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } echo randomFloat(0, 1); Fixed wrong range calculation and use $min / $max instead of $from / $to.