Ask the php high-concurrency lucky draw program, how to deal with repeated prizes and how many people draw the same prize
Source: Internet
Author: User
Ask the php high-concurrency lottery program, how to avoid repeated wins and multiple people draw the same prize? My practice is as follows: First Prize: One prize, second prize: two, third grade: Three. in this way, the prize is 6. if 100 people participate in the lucky draw at the same time, then the empty Prize is 100-6 = 94. First, query the database mysql for a unique number with 6 awards as the prize number: sql1: $ SQL = "ask for the php high concurrency lottery program, how can we avoid repeated prizes and multiple people winning the same prize?
My practice is as follows: First Prize: One prize, second prize: two, third grade: Three. in this way, the prize is 6. if 100 people participate in the lucky draw at the same time, then the empty Prize is 100-6 = 94.
First, query the database mysql for the unique number with 6 awards as the reward number:
Sql1:
$ SQL = "select unique number from table where status = 0 ";
Get the array $ real = array ('001', '002', '002', '002', '002', '001',), where 001,002 is a unique number.
Then 94 empty prizes are generated
$ Empty = array ('','',...,'',);
Finally, the array is merged to get a brand new array.
$ Arr = array_merge ($ real, $ empty );
The user randomly extracts an item from the array.
$ Rand = mt_rand (0, count ($ arr)-1 );
For example, $ rand received 001.
If (''! = $ Rand ){
// Indicates the data is being extracted and the data status is changed. you need to operate the database here.
Sql2:
$ SQL = "update table set status = 1 where unique id = 001 ";
Operation completed
} Else {
// Indicates that the image is not being extracted and no operation is performed.
}
The problem now is that when 100 people enter the lucky draw at the same time, sql1 will continue to use 001 as the undrawn award even when the red part of sql2, to allow other users to continue pumping, so 001 may be pulled again.
I would like to ask you to help the php high-concurrency lottery program. how can we avoid repeated prizes and multiple people winning the same prize?
Or consider the lottery program from other perspectives. Concurrent php lottery share:
------ Solution --------------------
1. generate prize pool $ arr = array_merge ($ real, $ empty );
2. stored in files or standalone tables
3. open a file or table in exclusive mode during the lucky draw (in the future, the lottery can only be queued to wait for the release of resources)
4. modify the prize pool and release resources
Whether it is an instant payment or a one-time expiration, it can be processed as a method.
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.