PHP Lottery algorithm

Source: Internet
Author: User
Tags vars

Original: http://blog.csdn.net/dongsg11200/article/details/24607877
PHP winning probability algorithm, can be used for scraping cards, large turntable and other lottery algorithm

  1. <?php
  2. /*
  3. * Classical probability algorithm,
  4. * $PROARR is a pre-set array,
  5. * Suppose array is: Array (100,200,300,400),
  6. * Start is to filter the first number from the probability range of 1,1000 within the range of his probability of occurrence,
  7. * If not, then the probability space, that is, the value of K minus the probability space of the number just now,
  8. * In this case, minus 100, which means the second number is filtered in the 1,900 range.
  9. * This filter to the end, there will always be a number to meet the requirements.
  10. * It's the equivalent of going to a box and touching something,
  11. * The first is not, the second is not, the third one is not, and the last one must be.
  12. * This algorithm is simple and highly efficient,
  13. * The key is that this algorithm has been used in our previous projects, especially in large data volumes, which are very efficient.
  14. */
  15. function Get_rand ($proArr) {
  16. $result = ";
  17. The total probability accuracy of probability array
  18. $proSum = array_sum ($proArr);
  19. Probability array loop
  20. foreach ($proArr as $key = = $proCur) {
  21. $randNum = Mt_rand (1, $proSum);
  22. if ($randNum <= $proCur) {
  23. $result = $key;
  24. Break
  25. } Else {
  26. $proSum-= $proCur;
  27. }
  28. }
  29. unset ($PROARR);
  30. return $result;
  31. }
  32. /*
  33. * Awards Array
  34. * is a two-dimensional array that records all the prize information for this draw,
  35. * The ID indicates the winning level, prize represents the prize, and V indicates the probability of winning.
  36. * Note that the V must be an integer, and you can set the V of the corresponding award to 0, which means that the odds of the prize being pumped are 0,
  37. * The sum of V in the array (cardinality), the larger the cardinality, the more the probability of accuracy can be expressed.
  38. * The sum of V in this example is 100, then the probability of winning the tablet corresponds to 1%,
  39. * If the sum of V is 10000, then the probability of winning is one out of 10,000.
  40. *
  41. */
  42. $prize _arr = Array (
  43. ' 0 ' = = Array (' id ' =>1,' prize ' = 'tablet ',' V ' =>1),
  44. ' 1 ' = = Array (' id ' =>2,' prize ' = 'digital camera ',' V ' =>5),
  45. ' 2 ' = = Array (' id ' =>3,' prize ' = 'speaker device ',' V ' =>10),
  46. ' 3 ' = = Array (' id ' =>4,' prize ' = ' 4G ', 'v ' =>12),
  47. ' 4 ' = = Array (' id ' =>5,' prize ' = ' 10Q ', 'v ' =>22),
  48. ' 5 ' = = Array (' id ' =>6,' prize ' = ' = 'next time it will be able to be in oh ',' V ' =>50),
  49. );
  50. /*
  51. * Each front page request, PHP Loop Award set Array,
  52. * Get the prize ID from the Get_rand by the probability calculation function.
  53. * Save winning prizes in array $res[' yes ',
  54. * While the remaining non-winning information is kept in $res[' no '),
  55. * Finally output JSON data to the front page.
  56. */
  57. foreach ($prize _arr as $key = + $val) {
  58. $arr [$val [' id ']] = $val [' V '];
  59. }
  60. $rid = Get_rand ($arr); //Get the prize ID based on probability
  61. $res [' yes '] = $prize _arr[$rid -1][' Prize ']; //In the awards
  62. unset ($prize _arr[$rid-1]); //The award is excluded from the array, leaving the award
  63. Shuffle ($prize _arr); //scrambled array order
  64. For ($i =0; $i <count ($prize _arr); $i + +) {
  65. $PR [] = $prize _arr[$i] [' Prize '];
  66. }
  67. $res [' no '] = $pr;
  68. Print_r ($res);
  69. ?>

PHP Lottery algorithm

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.