A full three years did not update the blog, today and girlfriend chat, chat to the blog, come back to see.
Recent contact with the lottery software, download the source code is http://download.csdn.net/detail/ghz_sd/6918125, here for open source software to contribute to the people pay tribute to the author of this software, a meter mountain, is a very good person, patiently help me Debug, His algorithm is very simple, is purely random, with the rand () function, I gave him a demand, write a cheat class, the realization of the function is: the probability of winning the specified person, the configuration file is similar to this:
< XML > < win > < name = ' a ' probability =20> <name = ' B ' probability =20> </win > </ XML >
If he realizes it, he will give him a good taste!!!
The lottery actually uses a random algorithm, which is summarized here
First, select the introduction of the algorithm
In the case of employment, if the candidate appears in a random order, the expected number of times to hire a new Office Assistant is lnn. The algorithm changes as the input changes. For a particular input, it will always
generate a fixed number of employments.
If the candidate is randomly arranged first, it randomly occurs on the algorithm instead of on the input distribution. Each time the algorithm is run, execution relies on a random selection, rather than relying on the input. This is the difference between a random algorithm and a probability analysis.
randomized-hire-assistant (n ) 1 randomly permute the List of Candidates2 best ←0 ®candidate 0 is a least-qualified dummy candidate3 for i ←1 to n 4 do Intervi EW candidate i 5 if candidate i Is better than candidate best 6 then best ←i 7 Hire candidate i
The stochastic algorithm is actually in the algorithm, the input is pre-reflow , the other exactly the same.
Here, there are 2 algorithms to rearrange the input, and they are randomly re-ranked. Both of these methods can produce uniform random permutations. (Each of these permutations is likely to be produced).
Pay special attention to the definition of Uniform random Arrangement!!!! The book below gives the 2 algorithms to rearrange the array, resulting in a uniform random arrangement, and give a strict mathematical proof, the theory is very high, we here this focus on the introduction and understanding of these two methods, do not delve into the proof, to see the proof can go to read.
--------------------------------------------------------------------------------------------------------------- ------------------------------------
Randomly arranged arrays:
1) permute-by-sorting (A)
Assigns a random priority to each element of the array, and then sorts the array a by priority.
Permute-by-sorting (anlength[afori to N3 do P[in
3
A P return A
This can obviously disrupt the order of the arrays, the key is that we have to prove that it is evenly randomly disturbed. This is based on the definition of uniform random arrangement, which proves that the probability of producing each permutation is 1/n!
Potential, we can know:
A) If an algorithm does not produce a total of n! Arrangement, then obviously it is not an even random permutation algorithm. An exercise after class
B) One might say, to prove that an arrangement is uniformly random, as long as the probability that each element appears at each location is 1/n. This is a misunderstanding of the uniform random arrangement. After class an exercise
Permute-by-sorting is difficult to see directly how many kinds of permutations can be produced.
2) randomize-in-place (a )
Loop over the array, each element A[i] and A[i] To an interchange in the last element (code below), it is easy to see that the total number of permutations is n! A
Randomize-in-place (anlength[afori to N3 do A[ia[RANDOM (in)]
for 1) 2) is a uniform random arrangement of the proof here is not spoken, the book has.
--------------------------------------------------------------------------------------------------------------- -----------------------------------
Exercises
(proving that the process of uniform random arrangement is very troublesome, we focus on the bottom of the line, a-b two, to determine whether an algorithm can produce uniform random arrangement on the line)
Professor 5.3.2 Kelp decided to use the following algorithms to randomly produce arbitrary permutations of a non-identical arrangement, did he realize his intention?
Permute-without-identity (anlength[aforito n3 do A[ia[RANDOM (in) ]
No, this piece of code can only produce (n-1)! The necessary conditions for reaching his intentions need to be able to produce n! -1 kinds of arrangement.
Point a) above.
5.3.3 assumes that it is not a random exchange of numbers on A[i] and A[I....N] (note that this is our correct algorithm), but each time it is randomly swapped with the number at any position on the array, can it produce a uniform random arrangement?
Permute-with-all (anlength[aforitoN 3 do A[ian)]
No, also point a), you can analyze it by yourself.
As a special case, suppose the arrangement length is n=3. Then call 3 random, and return one of the 3 values at a time, and there will be 27 possible results after executing the procedure permute-with-all. Because the arrangement length is n, there is a total of 3!=6 arrangement, if the Permute-with-all produces uniform random arrangement, then each permutation occurrence probability is 1/6. That is to say, each permutation should appear m times to make M/27=1/6. Because there is no integer m to make M/27=1/6, Permute-with-all does not necessarily produce uniform random permutations.
5.3.4 Armstrong recommends using the following procedure to produce a uniform random arrangement:
Permute-by-cyclic (A) 1N ←length[A]2Offset ←random (1,N) 3 for I ←1 to n4 do dest ← i + offset5 if D EST > n6 then dest ← dest -n7 B[ Dest] ← A[i]8 return B
It is proved that the probability of arbitrary elements appearing anywhere in B is 1/n, which proves that the algorithm can not produce uniform random arrangement.
1) Note that the 2nd line, offset in this is calculated, in the use of the time is a fixed value.
Offset can have n values, so the probability of any element appearing anywhere in B is 1/n.
2) This code can only produce n different permutations (the reason is still line 2nd)
This illustrates the dash part B)
Second, the probability of lottery-three algorithms
Click on the title to jump to the original blog
Three, the Platform lottery algorithm summary-no need to fear the prize is robbed http://www.xuanfengge.com/luck-draw.html (the front-end site has algorithmic articles)
Objective
Whenever the merchant engage in marketing activities, in order to reach more customers, to a little lottery tricks, it should be excellent, what "scraping music", "smashing golden eggs", "big turntable" and so on, the new, always tried. From the micro-customer multi-marketing platform of the use of various activities can also be seen, the lottery has been the most used by merchants online activities, is so-called no profit not to get up, to the point of "nectar" reward "bee" is also should.
Demand analysis
So the question comes, how does the awards mechanism play? As a service merchant's marketing platform, how to send the merchant's configured prizes to get a better effect?
Let's look at the goal first, what is the better effect, which is what the user (merchant) needs:
- The number of prizes is fixed during the lottery
- Rare prizes are drawn as far as possible.
- The prize does not wish to have the surplus
- Each prize can be set to the probability of being pumped
Scene simulation
For the sake of discussion, let's first assume the scenario:
Draw Time:
00:00:00-23:59:59
Prize Settings:
| Prize level |
Prize name |
Number of prizes |
| First prize |
A |
2 |
| Prize |
B |
3 |
| Third |
C |
4 |
Specific analysis
The first thing you can think of is to set the probability of winning each kind of prize, each time by setting a winning probability award, but the problem comes again:
The number of prizes fixed, but the number of participants in the lottery is unpredictable, there is no way to control the speed of the prize, if the probability set high, the lottery swarmed prizes are soon gone, set low, the prizes may not end. In addition, the probability of this technology-biased terminology users must understand a variety of, the use of communication costs are very high.
In fact, "Each prize can be set in the probability of being pumped" is a very vague saying that it is vague, mainly because you do not know where the probability of this setting is used, what the probability settings need to meet the conditions, the total number of samples (total draw number) is how much.
So the best thing to do is that the user doesn't have to care about the "probability of the prize being pumped", just the first three expected effects.
After analysis, we found that to reach the user above the three effect, as long as the prize in the event is gradually taken away, it can give each of the prizes set a allowed to be taken away from the time? Right! If you control the point at which each prize is issued, and then arrange the order in which prizes are issued, the prize will not be drawn from the outset until the final stage of the event is guaranteed.
Specific design
Following this idea, we look at the specific design:
Evenly bury the prize in the entire activity time (3600*24=86400 seconds), such as the above hypothetical scenario, a total of 9 prizes, the activity time evenly divided into 9 parts
Take the remaining number of prizes as weights, and randomly select each time period of the prize type (the color corresponding to the prizes see table above)
Randomly select the "release" Time point of the prize in each time period, one for uniformity, and the other to avoid direct exposure to precise points of time.
| 1 |
Releasetime(n)=startTime+(n-1) x ? T+random(? T) |
Implementation scenarios
After the idea, we look at the realization: in the prize release time after the lottery users have the opportunity (this probability can be configured, such as 100% or 80%) to get the prize of the time period, if the prize has not been taken away, will continue to wait for the lottery to arrive.
Idea One
The intuitive approach is to create three sheets
T_award_batch (prize Description table, used to record the configuration information of various prizes),
| Id |
Names (name) |
Total Prizes (amount) |
| 1 |
A |
2 |
| 2 |
B |
3 |
| 3 |
C |
4 |
T_award_pool (prize pool table, used to generate a time point for each prize release),
| Id |
Prize ID (award_id) |
Release Time (Release_time) |
Number of remaining (balance) |
| 1 |
3 |
1:03:27 |
0 |
| 2 |
2 |
3:15:13 |
1 |
| 3 |
3 |
5:29:57 |
1 |
| 4 |
2 |
9:35:34 |
1 |
| 5 |
1 |
12:57:20 |
1 |
| 6 |
2 |
13:47:03 |
1 |
| 7 |
3 |
17:31:50 |
1 |
| 8 |
1 |
18:13:26 |
1 |
| 9 |
3 |
22:28:40 |
1 |
T_record (Lottery record table for recording each lottery draw)
| Id |
Prize ID (award_id) |
Winning Time (Hit_time) |
User ID (owner_id) |
| 1 |
3 |
1:03:45 |
Peteryan |
Before the event begins, the data in the T_award_pool is initialized according to the prize configuration information in the T_award_batch, and the release time of each prize is initialized so that when the user comes to the lottery, according to the current time in T_award_ A query in the pool table to a prize that has been released and has not been pumped out
| 1 |
select id from t_award_pool where release_time <=< Span class= "crayon-h" > now () and balance > 0 limit 1 Span class= "Crayon-sy", |
After the query to update it, if it was stolen by others, did not win
| 1 |
Update T_award_pool set balance = balance – 1 where ID c16>= #{id} balance > 0; |
At the same time leave the lottery to T_record.
Two ideas
In the idea of a, in order to facilitate the lottery to determine whether there is currently a prize, to initialize the release time of each prize, when the number of prizes is smaller, the situation is good, for the number of prizes very much time, the lottery query time will increase, the initialization of the prize pool is time-consuming action, Whether or not to rely on this table to determine whether there is currently a prize release through real-time computation.
Add two fields to the T_award_batch table, the total amount of prizes remaining balance and the last winning time Last_update_time.
| Id |
Names (name) |
Total Prizes (amount) |
Prize allowance (balance) |
Update Time (Last_update_time) |
| 1 |
A |
2 |
2 |
1:03:45 |
| 2 |
B |
3 |
3 |
0:00:00 |
| 3 |
C |
4 |
3 |
0:00:00 |
This implementation only needs to rely on the prize configuration information, the sample code is as follows (click the Picture Full screen view):
Among them, Awardbatch represents a category of prizes, such as the first prize mentioned in the table above. In the above code, randomly select the next prize logic to be released as follows:
Through this set of awards mechanism, the target of marketing merchant is satisfied well, and the tangle of complex probability calculation is reduced, and the prize is robbed in advance.
Summary of stochastic algorithms thought by lottery software