Jump to content

Generate random floating point


MrXHellboy

Recommended Posts

Hi All,

 

I created this simple function to generate random doubles..... Please your opinions!

 

function GetRandomFloatNumber($min = false, $max = false, $dec_min = false, $dec_max = false)
{
// Seed the generator
mt_srand((double)microtime()*1000000);

// Set the default min and max if no parameter has been passed
if ($min === false)
	$min = 0;
if ($max === false)
	$max = mt_getrandmax();
if ($dec_min === false)
	$dec_min = 0;
if ($dec_max === false)
	$dec_max = mt_getrandmax();
// units
$units = mt_rand($min, $max);
// Decimals
$decimals = mt_rand($dec_min, $dec_max);
// Set double
$doublenumber = $units.'.'.$decimals;
// Return double
return $doublenumber;
}

Link to comment
https://forums.phpfreaks.com/topic/220123-generate-random-floating-point/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.