Simple random lottery applet based on PHP

Source: Internet
Author: User
An introduction to a lottery applet is controllable, and a prize can be displayed on the front end, which is impossible to be obtained at all! After adding all probability x10, the value of each item in the new array is equal to the sum of the first few items in the new array plus itself as a lucky draw program. The introduction is controllable, and a prize can also be displayed on the front end, but it is impossible to get it in the program! After adding all probability x10, the value of each item in the new array is equal to the sum of the first few and the addition of itself.

Then, a number between 0 and the maximum number is generated. you can view the range in a loop and return the key of the range.

The code is as follows:

$ Prize = array (1 => 3.5, // 3.5% probability 2 => 2.5, 3 => 6, 4 => 2, 5 => 42, // the probability of 42% is 6 => 36, 7 => 2, 8 => 4, 9 => 2 ); // finally confirm that the sum is equal to 100 $ prizeList = array (1 => array ('Little fairy '), 2 => array ('5q coin '), 3 => array ('000000'), 4 => array ('000000'), 5 => array ('thank you! '), 6 => array (' welcome to come back next time! '), 7 => array ('10q coin'), 8 => array ('1970 silver ingot (bound) '), 9 => array ('random advanced mounts (bound) '); // unset ($ prize [7]); directly remove the 10Q prize $ times = 10; $ max = 0; foreach ($ prize as $ k =>$ v) {$ max = $ v * $ times + $ max; $ row ['V'] = $ max; $ row ['K'] = $ k; $ prizeZone [] = $ row;} $ max --; // critical value $ rand = mt_rand (0, $ max ); $ zone = 1; foreach ($ prizeZone as $ k = >$ v) {if ($ rand >=$ v ['V']) {if ($ rand >=$ prizeZone [$ k + 1] ['V']) {continue ;} else {$ zone = $ prizeZone [$ k + 1] ['K']; break; }}$ zone = $ v ['K']; break ;} print_r ($ prizeList [$ zone] [0]);

Php-based implementation of simple code for the lucky draw applet is so much written for everyone, but it is all the core code and I hope it will be helpful for everyone to learn.

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.