Home  >  Article  >  Backend Development  >  A simple lottery test prototype

A simple lottery test prototype

WBOY
WBOYOriginal
2016-07-25 09:05:101084browse
Business needs, so I made a simple prototype test. The main requirement of the function is: the higher the amount in the user account, the greater the chance of winning the prize.
This script can only be used from the command line, usage example:
php lucky.php 100

  1. /**
  2. * Lottery program
  3. *
  4. * @File: lucky.php
  5. * @Author: zzxworld
  6. * @Email: zzxworld@gmail.com
  7. * @Date: 2012-05-09
  8. */
  9. $timer = microtime(true);
  10. /**Manually configured parameters **/
  11. # Prize pool size
  12. $lucky_size = 1000;
  13. # highest The winning probability of the user cannot exceed the prize pool size
  14. $star_user_rate = 100;
  15. # First prize bonus
  16. $lucky_first = 100;
  17. # Second prize bonus
  18. $lucky_second = 50;
  19. # Third prize bonus
  20. $lucky_third = 25;
  21. # Maximum normal prize amount
  22. $lucky_normal_max = 10;
  23. # Minimum normal prize amount
  24. $lucky_normal_min = 1;
  25. /**Parameters that automatically take values ​​from the system **/
  26. # Total bonus amount
  27. $lucky_amount = 578;
  28. # Owned The account amount of the user with the most assets
  29. $star_user_amount = 134;
  30. # Current user amount
  31. $user_amount = isset($argv[1]) ? intval($argv[1]) : 0;
  32. if ($user_amount < 1) {
  33. die('Please set a user amount after the command.' . chr(10));
  34. }
  35. # Calculate the current user's probability of winning
  36. $user_rate = $star_user_rate/$lucky_size/$star_user_amount*$user_amount;
  37. # Calculate the number of bonuses based on the probability of winning
  38. $lucky_num = intval($user_rate * $lucky_size);
  39. # Initialize the prize pool
  40. $lucky_pool = array_fill(0, $lucky_size, 0);
  41. # The probability of winning is less than 1 Adjust to 1
  42. if ($lucky_num < 1) {
  43. $lucky_num = 1;
  44. }
  45. # Arrange the prizes in the prize pool
  46. $lucky_items = array('first', 'second', 'third', 'normal' );
  47. $lucky_num += 3;
  48. # Remove the first, second and third prizes according to the total prize money
  49. if ($lucky_amount < $lucky_first) {
  50. $lucky_num -= 1;
  51. unset($lucky_items['first' ]);
  52. }
  53. if ($lucky_amount < $lucky_second) {
  54. $lucky_num -= 1;
  55. unset($lucky_items['second']);
  56. }
  57. if ($lucky_amount < $lucky_third) {
  58. $lucky_num -= 1;
  59. unset($lucky_items['third']);
  60. }
  61. # Randomly generate prizes in the prize pool
  62. foreach(array_rand($lucky_pool, $lucky_num) as $key) {
  63. $name = $lucky_items[array_rand($lucky_items)];
  64. if (!isset($create_lucky_first) && $name == 'first') {
  65. $lucky_pool[$key] = $lucky_first;
  66. $create_lucky_first = true;
  67. continue;
  68. }
  69. if (!isset($create_lucky_second) && $name == 'second') {
  70. $lucky_pool[$key] = $lucky_second;
  71. $create_lucky_second = true;
  72. continue;
  73. }
  74. if (!isset( $create_lucky_third) && $name == 'third') {
  75. $lucky_pool[$key] = $lucky_third;
  76. $create_lucky_third = true;
  77. continue;
  78. }
  79. if ($name == 'normal') {
  80. $ lucky_pool[$key] = rand($lucky_normal_min, $lucky_normal_max);
  81. }
  82. }
  83. # Lottery
  84. $result = $lucky_pool[rand(0, $lucky_size)];
  85. echo 'Prize pool size:' . $ lucky_size . chr(10);
  86. echo 'Total bonus amount:' . $lucky_amount . chr(10);
  87. echo 'First prize bonus:' . $lucky_first . chr(10);
  88. echo 'Second prize bonus: ' . $lucky_second . chr(10);
  89. echo 'Third prize bonus:' . $lucky_third . chr(10);
  90. echo 'Normal prize bonus:' . $lucky_normal_min . ' - ' . $lucky_normal_max . chr(10 );
  91. echo 'Total amount of funds for star users:' . $star_user_amount . chr(10);
  92. echo 'Probability of winning for star users:' . ($star_user_rate/$lucky_size) . chr(10) . chr(10);
  93. echo 'Current user's total funds:' . $user_amount . chr(10);
  94. echo 'Current user's probability of winning:' . $user_rate . chr(10);
  95. echo 'Current user's prize pool:';
  96. foreach( $lucky_items as $name) {
  97. switch($name) {
  98. case 'first':
  99. echo 'First Prize (1),';
  100. $lucky_num--;
  101. break;
  102. case 'second':
  103. echo ' Second Prize(1),';
  104. $lucky_num--;
  105. break;
  106. case 'third':
  107. echo 'Third Prize(1),';
  108. $lucky_num--;
  109. break;
  110. case 'normal' :
  111. echo 'Common prize(' . $lucky_num . ')';
  112. break;
  113. }
  114. }
  115. echo chr(10);
  116. echo 'Current user lottery result:';
  117. if ($result == $lucky_first) {
  118. echo 'First Prize' . $result . 'Yuan';
  119. } elseif($result == $lucky_second) {
  120. echo 'Second Prize' . $result . 'Yuan';
  121. } elseif($result = = $lucky_third) {
  122. echo 'Third Prize' . $result . 'Yuan';
  123. } elseif($result >= $lucky_normal_min && $result <= $lucky_normal_max) {
  124. echo 'Normal Prize' . $result . 'yuan';
  125. } else {
  126. echo 'No prize won';
  127. }
  128. echo chr(10);
  129. echo chr(10);
  130. echo 'Execution time:' . (microtime(true) - $timer ) . 'Seconds' . chr(10);
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn