Copy CodeThe code is as follows:
$setting = Array (
Black probability
0 = 0.99,
White probability
1 = 0.01,
);
Requires the GD Library
Header ("Content-type:image/png");
$im = Imagecreatetruecolor (or Die) ("Cannot Initialize new GD image Stream");
$white = Imagecolorallocate ($im, 255, 255, 255);
$start = Microtime (true);
for ($y =0; $y <256; $y + +) {
for ($x =0; $x <256; $x + +) {
if (random ($setting) = = = 1) {
Imagesetpixel ($im, $x, $y, $white);
}
}
}
$time = Microtime (True)-$start;
Header ("X-exec-time:". $time);
Imagepng ($im);
Imagedestroy ($im);
/**
* Full probability calculation
*
* @param array $p Array (' A ' =>0.5, ' B ' =>0.2, ' C ' =>0.4)
* @return String returns the key of the above array
* @author Lukin
*/
function Random ($ps) {
Static $arr = Array (); $key = MD5 (serialize ($ps));
if (!isset ($arr [$key])) {
$max = Array_sum ($ps);
foreach ($ps as $k = = $v) {
$v = $v/$max * 10000;
for ($i =0; $i < $v; $i + +) $arr [$key] = $k;
}
}
return $arr [$key][mt_rand (0,count ($arr [$key])-1)];
}
?>
Black point occurrence probability 99%, white point appears probability 1%, test result:
The above introduces the webgame PHP full probability operation function optimization version of Webgame development must, including the webgame aspect of the content, I hope that the PHP tutorial interested in a friend helpful.