Copy Code code as follows:
<?php
$setting = Array (
Black probability
0 => 0.99,
White probability
1 => 0.01,
);
Requires the GD Library
Header ("Content-type:image/png");
$im = Imagecreatetruecolor (256, 256) 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 <my@lukin.cn>
*/
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 spot probability 99%, white dot occurrence probability 1%, test result: