Php random lottery code

Source: Internet
Author: User
This article will share with you the code that uses php to calculate the random lottery probability based on the Prize weight. it is very useful. if you have similar requirements, you can refer to the following 1. initial data:

A higher weight indicates a higher probability of extraction.
[Prize 1, weight 5], [Prize 2, weight 6], [prize 3, weight 7], [prize 4, weight 2]

2. Procedure:

1) N = 5 + 6 + 7 + 2 = 20
2) Then, take the random number M from 1 to n.
3) define the weight range of each prize; 1: 1-5; 2: 6-11; 3: 12-18; and 4: 19-20.
4) If M is within the weight range of a prize, mark the prize to be extracted.

<? Php/*** Prize */class Prize {# ID public $ id = null; # weight public $ weight = null; # Prize name public $ name = null; # protected $ start = 0; # protected $ end = 0; public function _ construct ($ id, $ weight, $ name) {if (! $ Id) {throw new Exception ('prize ID is empty. ') ;}$ this-> id = $ id; $ this-> weight = $ weight? $ Weight: 0; $ this-> name = $ name? $ Name: 'Random prize '. $ id ;}# id public function getId () {return $ this-> id ;}# weight public function getWeight () {return $ this-> weight ;} # set the weight range public function setRange ($ start, $ end) {$ this-> start = $ start; $ this-> end = $ end ;} # determine whether a random number is in the public function inRange ($ num) {return ($ num >=$ this-> start) in the weight range) & ($ num <= $ this-> end) ;}/ *** prize pool */class PrizePoll implements IteratorAggregate, Count Able {# Prize set protected $ items = array (); # add public function addItem (Prize $ item) {$ this-> items [$ item-> getId ()] = $ item; return $ this ;}# delete prize public function removeItem ($ itemId) {if (isset ($ this-> items [$ itemId]) {unset ($ this-> items [$ itemId]);} return $ this;} # Update Prize public function updateItem (Prize $ item) {if (isset ($ this-> items [$ item-> getId ()]) {$ this-> items [$ item-> getId ()] = $ item;} Return $ this ;}# get all prizes public function getItems () {return $ this-> items ;}# all available prizes (if the weight is 0, it indicates that this prize will never be obtained.) public function getVisibleItems () {$ items = array (); foreach ($ this-> items as $ item) {if ($ item-> getWeight () {$ items [$ item-> getId ()] = $ item ;}} return $ items ;}# Countable :: count public function count () {return count ($ this-> items) ;}# IteratorAggregate: getIterator () public func Tion getIterator () {return new ArrayIterator ($ this-> items) ;}/ *** simple lucky draw class */class SimpleTurn {# prize pool protected $ poll = null; public function _ construct (PrizePoll $ poll) {if ($ poll) {$ this-> setPoll ($ poll) ;}# lottery public function run (PrizePoll $ poll) {$ poll = $ poll? $ Poll: $ this-> poll; if (! $ Poll) {throw new Exception ('award pool not initialized ');} if ($ poll-> count () <= 0) {throw new Exception ('empty prize pool ') ;}$ items = $ poll-> getVisibleItems (); if (count ($ items) <= 0) {throw new Exception ('empty prize pool ');} $ sum = 0; foreach ($ items as $ item) {$ start = $ sum + 1; $ sum + = $ item-> getWeight (); $ end = $ sum; # set the weight range of the prize $ item-> setRange ($ start, $ end );} # Random number $ rand = $ this-> getRandNum (1, $ sum); # foreach ($ ite MS as $ item) {if ($ item-> inRange ($ rand) {return $ item;} return null ;}# obtain the random number public function getRandNum ($ min, $ max) {return mt_rand ($ min? $ Min: 1, $ max) ;}# set public function setPoll (PrizePoll $ poll) {$ this-> poll = $ poll ;}} # Example try {$ prizePoll = new PrizePoll (); $ prizePoll-> addItem (new Prize (1, 5)-> addItem (new Prize (2, 6 )) -> addItem (new Prize (3, 7)-> addItem (new Prize (4, 2); $ turn = new SimpleTurn ($ prizePoll ); $ prize = $ turn-> run (); var_dump ($ prize);} catch (Exception $ e) {print_r ($ e );}

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.