How can I set points on a 24h period spreaded by the Gaussian distributions? For example to have the peak at 10 o'clock?
+2
A:
If you have trouble generating gaussian distributed random points look up http://en.wikipedia.org/wiki/Box-Muller_transform
Else please clarify your question.
Mastermind
2008-09-30 08:26:03
+5
A:
The following code generates a gaussian distributed random time (in hours, plus fractions of an hour) centered at a given time, and with a given standard deviation. The random times may 'wrap around' the clock, especially if the standard deviation is several hours: this is handled correctly. A different 'wrapping' algorithm may be more efficient if your standard deviations are very large (many days), but the distribution will be almost uniform in this case, anyway.
$peak=10; // Peak at 10-o-clock
$stdev=2; // Standard deviation of two hours
$hoursOnClock=24; // 24-hour clock
do // Generate gaussian variable using Box-Muller
{
$u=2.0*mt_rand()/mt_getrandmax()-1.0;
$v=2.0*mt_rand()/mt_getrandmax()-1.0;
$s = $u*$u+$v*$v;
} while ($s > 1);
$gauss=$u*sqrt(-2.0*log($s)/$s);
$gauss = $gauss*$stdev + $peak; // Transform to correct peak and standard deviation
while ($gauss < 0) $gauss+=$hoursOnClock; // Wrap around hours to keep the random time
$result = fmod($gauss,$hoursOnClock); // on the clock
echo $result;
Chris Johnson
2008-09-30 13:27:11
I think this form of the algorithm is called “Marsaglia polar method”.
lapo
2009-11-21 18:16:26