Webgame PHP Full probability op function optimized version webgame development essentials

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.