PHP probability calculation function summary, php probability function _ PHP Tutorial

Source: Internet
Author: User
PHP probability calculation function summary, php probability function. Summary of PHP probability computing functions. php probability functions are actually useless in this blog. it's too simple for people to think about it. no one else can think about it. But looking at the summary of PHP probability computing functions, php probability functions

In fact, this blog is useless. it's too simple. people will not be able to think about it, and even people will think about it. But it's really bad to watch my blog for so long ~. In the next article, only the code of hydrology occupies at least 10 KB of OSC database space.

<? Php/*** Probability calculation class * can be used for lottery */class Probability {/*** Probability statistics ** thing => chance */var $ data = array (); var $ chance_count = 0; function _ construct ($ initdata = array () {if (! Empty ($ initdata) {$ this-> data = $ initdata; foreach ($ initdata as $ d) {$ this-> chance_count + = $ d ['num'] ;}} function addData ($ name, $ chance) {$ this-> data [] = array ('name' => $ name, 'num' => $ chance); $ this-> chance_count + = $ chance ;} function getOne () {$ index = rand (0, $ this-> chance_count); foreach ($ this-> data as $ d) {$ index = $ index-$ d ['num']; if ($ index <= 0) {return $ d ['name'] ;}} return '';}/*** example of use */$ pro = new Probability (); $ pro-> addData ('iPhone ', 10 ); $ pro-> addData ('Watch ', 30); $ pro-> addData (' $18 ', 50); $ pro-> addData ('thank you ', 10); $ pro-> addData ('Super big ', 1); for ($ I = 0; $ I <100; $ I ++) {echo $ pro-> getOne (). "\ n ";}

This is a classic probability algorithm function:

Function get_rand ($ proArr) {$ result = ''; // The total probability precision of the probability array $ proSum = array_sum ($ proArr ); // The probability array loop foreach ($ proArr as $ key => $ proCur) {$ randNum = mt_rand (1, $ proSum ); // extract random number if ($ randNum <= $ proCur) {$ result = $ key; // obtain the result break;} else {$ proSum-= $ proCur ;}} unset ($ proArr); return $ result ;}

Assume that we have an array like this: a prize has a probability of 20%, B Prize has a probability of 30%, and c prize has a probability of 50%.

$prize_arr =array('a'=>20,'b'=>30,'c'=>50);

Simulate the function execution process:

Total probability accuracy: 20 + 30 + 50 = 100

The first array loop, $ procur = 20

Assume that the random number rand (1,100) is extracted and $ randNum = 55 is obtained.

If judgment -------

If $ randNum <= 20, result =

Otherwise, the system enters the next cycle, and the total probability precision is changed to 100-20 = 80.

The second number of cycles, $ procur = 30

Assume that the random number rand () is extracted and $ randNum = 33 is obtained.

If Judge ---------

If $ randNum <= 30, result = B

Otherwise, enter the next cycle, and the total probability precision will change to 80-30 = 50.

The third number of group loops, $ prosur = 50;

Assume that the random number rand () is extracted. <或=50,< p>

Result = c;

Because the sample has not changed, although more than one random number may be extracted, the probability remains unchanged.

Alternatively, you can:

  function get_rand($arr)  {    $pro_sum=array_sum($arr);    $rand_num=mt_rand(1,$pro_sum);    $tmp_num=0;    foreach($arr as $k=>$val)    {        if($rand_num<=$val+$tmp_num)      {        $n=$k;        break;      }else      {        $tmp_num+=$val;      }    }    return $n;  }

I will share with you a probability algorithm for drawing.

/** Classic probability algorithm. * $ proArr is a preset array. * assume that the array is array (100,200,300,400 ), * The start is to check whether the first number is within the probability range of 1,. * If not, the probability space is used, that is, the value of k minus the probability space of the number just now. * In this example, we subtract 100, that is, the second number is filtered within the range of 1,900. * In this way, there will always be a number that meet the requirements. * It is equivalent to touching something in a box. * The first is not, the second is not, and the third is not. The last one must be. * This algorithm is simple and highly efficient. * The key is that it has been used in our previous projects, especially in projects with large data volumes. */Function get_rand ($ proArr) {$ result = ''; // The total probability precision of the probability array $ proSum = array_sum ($ proArr ); // probability array loop foreach ($ proArr as $ key => $ proCur) {$ randNum = mt_rand (1, $ proSum); if ($ randNum <= $ proCur) {$ result = $ key; break;} else {$ proSum-= $ proCur ;}} unset ($ proArr); return $ result ;} /** award array * is a two-dimensional array that records all prize information for this lottery. * The id indicates the winning level, the prize indicates the prize, and the v indicates the winning probability. * Note that the value of v must be an integer. you can set the value of v for the corresponding award to 0, which means that the percentage of the award is 0, * The sum (base number) of values in the array. the larger the base number, the more accurate the probability is. * In this example, the sum of v is 100, and the probability of winning a tablet is 1%. * If the sum of v is 10000, the probability of winning a tablet is one in ten. **/$ Prize_arr = array ('0' => array ('id' => 1, 'prize' => 'tablet ', 'V' => 1 ), '1' => array ('id' => 2, 'prize' => 'digital camera ', 'V' => 5 ), '2' => array ('id' => 3, 'prize' => 'Speaker device', 'V' => 10 ), '3' => array ('id' => 4, 'prize' => '4G disks', 'V' => 12 ), '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ), '5' => array ('id' => 6, 'prize' => 'next time you may be able to get in,', 'V' => 50 ),); /** for each request on the front-end page, PHP loop Awards are set as an array. * The probability calculation function get_rand is used to obtain the prize id. * Save the prize to the array $ res ['yes'], and * save the remaining unwon information in $ res ['no, * output the json data count to the front-end page. */Foreach ($ prize_arr as $ key => $ val) {$ arr [$ val ['id'] = $ val ['V'];} $ rid = get_rand ($ arr); // Obtain the award id based on the probability $ res ['yes'] = $ prize_arr [$ rid-1] ['prize']; // Medium Award unset ($ prize_arr [$ rid-1]); // removes the Medium Award from the array, and the remaining unmiddle award shuffle ($ prize_arr ); // disrupt the array order for ($ I = 0; $ I
 
  

Actually, this blog post is useless. it's too simple. people who will think about it will not think about it. people who will not think about it will also think about it. But look...

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.