Golden eggs hit probability

Source: Internet
Author: User


Function getrand ($ 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;
}

 

 

 

 

 

 

<? PHP
$ Prize_arr = array (
'0' => array ('id' => 1, 'prize' => 'tablet pc', 'V' => 3 ),
'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' => 'q coin 10 yuan ', 'V' => 20 ),
'5' => array ('id' => 6, 'prize' => 'Next time you may be able to get in, ', 'V' => 50 ),
);


Foreach ($ prize_arr as $ key => $ Val ){
$ Arr [$ Val ['id'] = $ Val ['V'];
}
// Print_r ($ ARR );

$ Rid = getrand ($ ARR); // obtain the award id based on the probability.
$ Res ['msg '] = ($ rid = 6 )? ;
$ Res ['prize'] = $ prize_arr [$ RID-1] ['prize']; // medium Award
Echo json_encode ($ res); exit;

?>

 

Self-Use

 

$ Prize_arr = array (
'0' => array ('id' => 1, 'prize' => '1 point', 'V' => 90 ),
'1' => array ('id' => 2, 'prize' => '10 credits ', 'V' => 9 ),
'2' => array ('id' => 3, 'prize' => '123', 'V' => 1)
);

Foreach ($ prize_arr as $ key => $ Val ){
$ Arr [$ Val ['id'] = $ Val ['V'];
}

$ Rid = getrand ($ ARR );
$ _ DATA ['title'] = $ prize_arr [$ RID-1] ['prize']; // medium Award

 

Golden eggs hit probability

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.