I wrote PHP-code that gets me samples of a (truncated) exponential distribution between 0 and 1 with mean 1 ($X\sim \mathrm{Exp}(1)$).
I'm trying to use acceptance-rejection method: I don't know if I get it right, but basically I use one sample of a $\mathrm{Uniform}(0,1)$ distribution to get the x-axis and another to get the y-axis and then check if the y-axis sample is below (valid sample) or above (reject sample) the exponential distribution function curve.
How do I get it to give me samples of a generic $X\sim \mathrm{Exp}(\lambda)$?
$num_samples=1000;
$samples=array();$sample=0;$count=0;$counts=0;
$time=microtime(true);
for($i=0;$i<$num_samples;$i++){
while (exp(-($sample = mt_rand(0,PHP_INT_MAX)*(1/PHP_INT_MAX)))
<
mt_rand(0,PHP_INT_MAX)*(1/PHP_INT_MAX)){
$count++;
}
$samples[] = $sample;
$counts+=$count;
$count=0;
}
echo "samples: ".$num_samples." time: ".(microtime(true)-$time)." efficiency: ".$num_samples/$counts."\n";