how can I regenerate random decimal from -0.0010 to 0.0010 with php rand()? or any other?
Now you've really limited to 21 possible values. But perhaps that's what benmsia wants...
VolkerK
2010-07-08 18:20:33
yeah. thx. tho more values generated will be better but if there's no valid solution then i will stick to this temporarily till a new solution is found. thanks nathan.
benmsia
2010-07-08 19:07:28
+1
A:
This will return any possible number between -0.001
and +0.001
$random = ((rand()*(0.002/getrandmax()))-0.001)
// or without paranthesis:
$random = rand()*0.002/getrandmax()-0.001
jigfox
2010-07-08 18:13:17
@jens, hi thanks for your input. but it generated a -ve 2.plus value for unknown reason. you have any idea?
benmsia
2010-07-08 19:04:29
+2
A:
Devide rand()
by the maximum rand-numer, multiply it by the range and add the starting number:
<?php
// rand()/getrandmax() gives a float number between 0 and 1
// if you multiply it by 0.002 you'll get a number between 0 and 0.002
// add the starting number -0.001 and you'll get a number between -0.001 and 0.001
echo rand()/getrandmax()*0.002-0.001;
?>
Harmen
2010-07-08 18:15:13
Hi Harmen, don't know what reason fomr ur formula i have found a bug there. it generated a -ve 2.plus value.. u have any idea? thanks
benmsia
2010-07-08 19:01:40
I guess you found a number close to zero, like `-4.7090060121464E-5`. It means `-4.7*10^-5` which is `-0.000047`
Harmen
2010-07-08 19:07:33