A simple lottery test prototype
Release: 2016-07-25 09:05:10
Original
1122 people have browsed it
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
- /**
- * Lottery program
- *
- * @File: lucky.php
- * @Author: zzxworld
- * @Email: zzxworld@gmail.com
- * @Date: 2012-05-09
- */
- $timer = microtime(true);
- /**Manually configured parameters **/
- # Prize pool size
- $lucky_size = 1000;
- # highest The winning probability of the user cannot exceed the prize pool size
- $star_user_rate = 100;
- # First prize bonus
- $lucky_first = 100;
- # Second prize bonus
- $lucky_second = 50;
- # Third prize bonus
- $lucky_third = 25;
- # Maximum normal prize amount
- $lucky_normal_max = 10;
- # Minimum normal prize amount
- $lucky_normal_min = 1;
- /**Parameters that automatically take values from the system **/
- # Total bonus amount
- $lucky_amount = 578;
- # Owned The account amount of the user with the most assets
- $star_user_amount = 134;
- # Current user amount
- $user_amount = isset($argv[1]) ? intval($argv[1]) : 0;
- if ($user_amount < 1) {
- die('Please set a user amount after the command.' . chr(10));
- }
- # Calculate the current user's probability of winning
- $user_rate = $star_user_rate/$lucky_size/$star_user_amount*$user_amount;
- # Calculate the number of bonuses based on the probability of winning
- $lucky_num = intval($user_rate * $lucky_size);
- # Initialize the prize pool
- $lucky_pool = array_fill(0, $lucky_size, 0);
- # The probability of winning is less than 1 Adjust to 1
- if ($lucky_num < 1) {
- $lucky_num = 1;
- }
- # Arrange the prizes in the prize pool
- $lucky_items = array('first', 'second', 'third', 'normal' );
- $lucky_num += 3;
- # Remove the first, second and third prizes according to the total prize money
- if ($lucky_amount < $lucky_first) {
- $lucky_num -= 1;
- unset($lucky_items['first' ]);
- }
- if ($lucky_amount < $lucky_second) {
- $lucky_num -= 1;
- unset($lucky_items['second']);
- }
- if ($lucky_amount < $lucky_third) {
- $lucky_num -= 1;
- unset($lucky_items['third']);
- }
- # Randomly generate prizes in the prize pool
- foreach(array_rand($lucky_pool, $lucky_num) as $key) {
- $name = $lucky_items[array_rand($lucky_items)];
- if (!isset($create_lucky_first) && $name == 'first') {
- $lucky_pool[$key] = $lucky_first;
- $create_lucky_first = true;
- continue;
- }
- if (!isset($create_lucky_second) && $name == 'second') {
- $lucky_pool[$key] = $lucky_second;
- $create_lucky_second = true;
- continue;
- }
- if (!isset( $create_lucky_third) && $name == 'third') {
- $lucky_pool[$key] = $lucky_third;
- $create_lucky_third = true;
- continue;
- }
- if ($name == 'normal') {
- $ lucky_pool[$key] = rand($lucky_normal_min, $lucky_normal_max);
- }
- }
- # Lottery
- $result = $lucky_pool[rand(0, $lucky_size)];
- echo 'Prize pool size:' . $ lucky_size . chr(10);
- echo 'Total bonus amount:' . $lucky_amount . chr(10);
- echo 'First prize bonus:' . $lucky_first . chr(10);
- echo 'Second prize bonus: ' . $lucky_second . chr(10);
- echo 'Third prize bonus:' . $lucky_third . chr(10);
- echo 'Normal prize bonus:' . $lucky_normal_min . ' - ' . $lucky_normal_max . chr(10 );
- echo 'Total amount of funds for star users:' . $star_user_amount . chr(10);
- echo 'Probability of winning for star users:' . ($star_user_rate/$lucky_size) . chr(10) . chr(10);
- echo 'Current user's total funds:' . $user_amount . chr(10);
- echo 'Current user's probability of winning:' . $user_rate . chr(10);
- echo 'Current user's prize pool:';
- foreach( $lucky_items as $name) {
- switch($name) {
- case 'first':
- echo 'First Prize (1),';
- $lucky_num--;
- break;
- case 'second':
- echo ' Second Prize(1),';
- $lucky_num--;
- break;
- case 'third':
- echo 'Third Prize(1),';
- $lucky_num--;
- break;
- case 'normal' :
- echo 'Common prize(' . $lucky_num . ')';
- break;
- }
- }
- echo chr(10);
- echo 'Current user lottery result:';
- if ($result == $lucky_first) {
- echo 'First Prize' . $result . 'Yuan';
- } elseif($result == $lucky_second) {
- echo 'Second Prize' . $result . 'Yuan';
- } elseif($result = = $lucky_third) {
- echo 'Third Prize' . $result . 'Yuan';
- } elseif($result >= $lucky_normal_min && $result <= $lucky_normal_max) {
- echo 'Normal Prize' . $result . 'yuan';
- } else {
- echo 'No prize won';
- }
- echo chr(10);
- echo chr(10);
-
- echo 'Execution time:' . (microtime(true) - $timer ) . 'Seconds' . chr(10);
Copy code
|
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31