PHP algorithm for calculating lottery probability

Source: Internet
Author: User
This article mainly introduces PHP programming to achieve the calculation of lottery probability algorithm, combined with a complete example of the PHP random number operation related operation skills, the need for friends can refer to the next

Specific as follows:

<?php//calculation of the probability function Get_rand ($PROARR) {$result = "; $proSum = Array_sum ($PROARR); foreach ($proArr as $key = =)  $proCur) {$randNum = Mt_rand (1, $proSum);  if ($randNum <= $proCur) {$result = $key;  Break  } else {$proSum-= $proCur; }} unset ($PROARR); return $result; } $prize _arr = Array (' 0 ' = = Array (' ID ' =>1, ' prize ' = ' home appliance ', ' V ' =>2), ' 1 ' = = Array (' ID ' =>2, ' prize ' = ') Digital camera ', ' V ' =>5), ' 2 ' = = Array (' ID ' =>3, ' prize ' = ' iPad ', ' V ' =>13), ' 3 ' = = Array (' ID ' =>4, ' prize ' = ' LED display ', ' V ' =>15, ' 4 ' = = Array (' ID ' =>5, ' prize ' = ' u disk ', ' V ' =>25), ' 5 ' = = Array (' ID ' =>6, ' prize ' = > ' keyboard ', ' V ' =>30), ' 6 ' = = Array (' ID ' =>7, ' prize ' = ' mouse pad ', ' V ' =>10),); foreach ($prize _arr as $key + $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); $res [' yes '] = $prize _arr[$rid -1][' Prize ']; unset ($prize _arr[$rid-1]); Shuffle ($prize _arr); $prize _arrcount = count ($prize _arr); for ($i =0; $i < $prize _arrcount; $i + +) {$PR [] = $prize_arr[$i [' Prize ']; } $res [' no '] = $PR; Lottery results $ro = $res [' Yes ']; Print_r ($ro);? >

Operation Result:

Ipad

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.