If you have a standard deviation, calculate the minimum value as: $Mean-9*$StdDev.

You can skew the curve to one side by supplying uneven min and max values as:
$RandomValue = randnorm($Median, $MinValue, $MaxValue);
The output will have a natural distribution, but the devations on each side will be uneven. It will have one tail longer than the other.


This function produces random values with a natural distribution (bell curve).

The Code

I put the code right up front for those who want it. Copy this into your PHP code. I personally save it in a file called randnorm.inc and use require_once('randnorm.inc') in my common includes section.

function randnorm($med, $min, $max=null) {
	$ldev = ($med-$min)/9;
	if($max == null) $max = $med+$med-$min;
	$rdev = ($max-$med)/9;
	$range = $max-$min;
	$ld = -2*pow(2*$ldev,2);
	$rd = -2*pow(2*$ldev,2);
	do {
		$x = rand()/getrandmax()*$range+$min;
		if($x<$med) {
			$y = rand()/getrandmax();
			$c = pow(M_E,pow($x-$med,2)/$ld);
		} else {
			$y = rand()/getrandmax();
			$c = pow(M_E,pow($x-$med,2)/$rd);
	} while($y>$c);
	return $x;


If you want a natural distribution around a mean, use:
$RandomValue = randnorm($Mean, $MinValue);

You've read it. You can't unread it.
Copyright ©1998-2019 C. Shaun Wagner. All rights reserved.