function count_decimals($x){ return strlen(substr(strrchr($x+"", "."), 1));}function random($min, $max){ echo "Min $min\n";//return as Min 4.008E-7 echo "Max $max\n";//return as Max 4.02E-7 die(); $decimals = max(count_decimals($min), count_decimals($max)); $factor = pow(10, $decimals); return rand($min*$factor, $max*$factor) / $factor;}$answer = random(0.0000004008, 0.000000402);echo "\n" . $answer . "\n";
The value send in for $min and $max is send in as scientific notation, thus the result return as invalid decimal place, when I did an echo for the result.
How do I fix such that the function able to return the random decimal between the 2 min, max, expected response should be
between
0.0000004009 to 0.0000004019 (smaller than 0.000000402, and bigger than 0.0000004008)