生成正态分布随机数

list=$list;
		$this->mu=$this->getMu($list);//活取平均值
		$this->sigma=$this->getSima($list);
	}
	/**
	 * @name 正态分布的累计概率函数
	 * @param  string|integer $value
	 * @return number
	 */
	public function getCdf($value){
		$mu = $this->mu;
		$sigma = $this->sigma;
		$t = $value-$mu;
		@$y =0.5*$this->erfcc(-$t/($sigma*sqrt(2.0)));
		if($y>1.0) $y=1.0;
		return $y;
	}
	public function erfcc($x){
		$z =abs($x);
		$t =1./(1.+0.5*$z);
		$r =$t*exp(-$z*$z-1.26551223+
             $t*(1.00002368+
             $t*(.37409196+
             $t*(.09678418+
             $t*(-.18628806+
             $t*(.27886807+
             $t*(-1.13520398+
             $t*(1.48851587+
             $t*(-.82215223+
             $t*.17087277)))))))));
		if($x>=0){
			return $r;
		}else{
			return 2-$r;
		}
	}
	/**
	 * @name 获取平均值
	 * @param  [array] $list 计算的数组
	 * @return [number]  计算的平均值
	 */
	public function getMu($list){
		return array_sum($list)/count($list);
	}
	/**
	 * @name 获取标准差
	 * @param  [array] $list 计算的数组
	 * @return [number] 方差的平方根,既标准差
	 */
	public function getSigma($list){
		$total_var = 0;
		foreach($list as $v){
			$total_var += pow(($v-$this->getMu($list)),2);
		}
		return sqrt($total_var/(count($list)-1));//这里数组个数为什么要减去1
	}

	/**
	 * @name 概率出现某个键值对
	 * @param  $proArr [array] 一维数组 例如奖品名=>奖品概率
	 * @return $result [string] 奖品名   
	 */
	function get_rand($proArr){
	$result = '';
	$proSum = array_sum($proArr);
	foreach($proArr as $key => $val){
		$randNum = mt_rand(1,$proSum);
		if($randNum<=$val){
			$result = $key;
			break;
		}else{
			$proSum -=$val;
		}
	}
	unset($proArr);
	return $result;
	}

	/**
	 * [getRandom]
	 * @param  [Array] $array 数值组
	 * @param  [String] $rate  概率组
	 * @return [type]        [description]
	 */
	function getRandom($array,$rate){
		$rate = explode(':',$rate);
		$sum = 0;$left = 0;$right = 0;
		foreach($rate as $value){
			$sum+=$value*10;
		}
		$temp = rand(0,$sum);
		foreach($rate as $key => $value){
			$right+=$value*10;
			if($left<=$temp&&$right>=$temp){
				return $array[$key];
			}
			$left+=$value*10;
		}
	}	
}



//use
$list = array(1.09,1.50,1.31,1.44);
$normdist = new Normdist($list);
echo $normdist->getCdf($list[0]);


//use
$price_arr = array('a'=>20,'b'=>30,'c'=>10,'d'=>40);
print($normdist->get_rand($price_arr));




//use
//$array = array(0,1,2,3,4,5);
$array = array('萝卜','苹果','石榴','番茄','柚子','椰子');
$rate = '2:1:1:1:2:3';
$a=$b=$c=$d=$e=$f=0;
for($i=0;$i<1100;$i++){
	if($normdist->getRandom($array,$rate)==$array[0]){
		echo $array[0];
		$a++;
	}
	if($normdist->getRandom($array,$rate)==$array[1]){
		echo $array[1];
		$b++;
	}
	if($normdist->getRandom($array,$rate)==$array[2]){
		echo $array[2];
		$c++;
	}
	if($normdist->getRandom($array,$rate)==$array[3]){
		echo $array[3];
		$d++;
	}
	if($normdist->getRandom($array,$rate)==$array[4]){
		echo $array[4];		
		$e++;
	}
	if($normdist->getRandom($array,$rate)==$array[5]){
		echo $array[5];
		$f++;
	}		
}

    echo $a;
    echo "\n";
    echo $b;
    echo "\n";
    echo $c;
    echo "\n";
    echo $d;
    echo "\n";
    echo $e;
    echo "\n";
    echo $f;
    echo "\n";

你可能感兴趣的:(PHP)