Home >Backend Development >PHP Tutorial >A simple probability-related code implemented in php, php probability code_PHP tutorial
The example in this article describes a simple probability-related code implemented in php. Share it with everyone for your reference, the details are as follows:
<?php for($i=1;$i<100000;$i++){ $x=mt_rand(0,100); if($x<20){ $a=$a+1; } else{ if($x<50){ $b=$b+1; } else{ $c=$c+1; } } } echo $a . '<br />'; echo $b . '<br />'; echo $c . '<br />'; ?>
Output result:
19589
29795
50615
The key is mt_rand(0,100)
Reflect the probability by the number of occurrences of $a $b $c
Additional: The above code is not formatted, which makes it difficult to read. Fortunately, there is not much code. The editor here recommends a typesetting tool for PHP formatting and beautification on this website to help you layout the code in future PHP programming and increase the readability of the code:
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
In addition, Since php belongs to C language style, the following tool can also format php code :
C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json
Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP mathematical operation skills", "Summary of PHP operating office document skills (including word, excel, access, ppt)", "PHP array ( Array) operating skills collection", "php sorting algorithm summary", "php common traversal algorithms and techniques summary", "php data structure and algorithm tutorial", "php programming algorithm summary", "php regular expression usage summary", "Summary of PHP operations and operator usage", "Summary of PHP string usage" and "Summary of common PHP database operation skills"
I hope this article will be helpful to everyone in PHP programming.