note 99859 modified in function.rand by amir

From: Date: Fri, 10 Sep 2010 21:44:15 +0000
Subject: note 99859 modified in function.rand by amir
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172466@lists.php.net to get a copy of this message
Random integers with normal distribution, it's not scientifically approved, but worked for me. <?php /* * @param float $mean, desired average * @param number $sd, number of items in array * @param number $min, minimum desired random number * @param number $max, maximum desired random number * @return array */ function array_distribute($mean,$sd,$min,$max){ $result = array(); $total_mean = intval($mean*$sd); while($sd>1){ $allowed_max = $total_mean - $sd - $min; $allowed_min = intval($total_mean/$sd); $random = mt_rand(max($min,$allowed_min),min($max,$allowed_max)); $result[]=$random; $sd--; $total_mean-=$random; } $result[] = $total_mean; return $result; } ?> --was-- Random integers with normal distribution, it's not scientifically approved, but worked for me. <?php /* * @param number $mean, desired average * @param number $sd, number of items in array * @param number $min, minimum desired random number * @param number $max, maximum desired random number * @return array */ function array_distribute($mean,$sd,$min,$max){ $result = array(); $total_mean = intval($mean*$sd); while($sd>1){ $allowed_max = $total_mean - $sd - $min; $allowed_min = intval($total_mean/$sd); $random = mt_rand(max($min,$allowed_min),min($max,$allowed_max)); $result[]=$random; $sd--; $total_mean-=$random; } $result[] = $total_mean; return $result; } ?> http://php.net/manual/en/function.rand.php

« previous php.notes (#172466) next »