PHP lottery probability random number

Source: Internet
Author: User

<?php$prize_arr = Array (' 0 ' = = Array (' id ' = = 1, ' title ' = ' iphone5s ', ' V ' = 5), ' 1 ' = = Array (      ' id ' = 2, ' title ' = ' Contact notebook ', ' V ' + ', ' 2 ' = = Array (' id ' = + 3, ' title ' = ' Speaker device ', ' V ' = 20), ' 3 ' = array (' id ' = = 4, ' title ' = ' 30GU ', ' V ' = ') ', ' 4 ' = = ' Array (' id ' = = 5, ' title ' = ' = ')  Call $50 ', ' V ' = +, ' 5 ' = = Array (' id ' = + 6, ' title ' = ' + ' iphone6s ', ' V ' = +), ' 6 ' = = Array (' ID ') + 7, ' title ' = ' Thank you, continue to refuel Oh!  ~ ', ' V ' = 10),);  foreach ($prize _arr as $key = + $val) {$arr [$val [' id ']] = $val [' V '];} $prize _id = Getrand ($arr); Get the prize ID according to probability $data [' msg '] = ($prize _id = = 7)? 0:1; If 0 is not $data [' prize_title '] = $prize _arr[$prize _id-1][' title ']; Winning prizes echo Json_encode ($data); Exit     Returns a JSON array to the front-end Function Getrand ($PROARR) {//calculates the winning probability $rs = ';//z winning result $proSum = Array_sum ($PROARR);//probability array total probability precision       Probability array loop foreach ($proArr as $key = = $proCur) {  $randNum = Mt_rand (1, $proSum);             if ($randNum <= $proCur) {$rs = $key;         Break         } else {$proSum-= $proCur;     }} unset ($PROARR); return $rs; }?>

PHP lottery probability random number

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.