Lucky draw probability-three kinds of algorithms

Source: Internet
Author: User
Tags arrays current time rand


First, every "several" winning

Every "several" winning, that is, by estimating the number of lottery and prizes to judge, "a few" = (Draw number/prize number) *n. This is a simple lottery algorithm, suitable for a large number of lottery, and no connection between the situation. Today's popular microblog-forwarding awards often use this algorithm, which determines the attribution of prizes, transparently and with incentives, based on the number of forwards.



Of course, this "a few" is not only the number of times, but also may be time, at a certain point in time can be pumped, but this program may lead to no one won the lottery and a lot of people to win the situation, time-point arrangement is crucial. This time point once announced, that is the second kill, rattling.



Every "several" winning has a lot of drawbacks, but very simple, very easy to achieve, by a lot of lottery activities adopted, some will announce the lottery rules, Incentive sweepstakes, some will not be published, in fact, the background operation may also be the algorithm, simple and efficient without losing fairness. In the case of opaque information, the ghost knows you are the first few lottery, haha.



Second, probability lottery


The so-called probability lottery is the most easy to think of the lottery algorithm, the probability can be static, it can also be changed in the adjustment, the most difficult is the use of how much probability, under what circumstances to adopt what probability. This is not a common scenario, different scenarios, the probability algorithm used is different. Here is an algorithm, based on the expiration date of the prize to calculate the winning rate of its current time, when the time is gradually approaching the expiry of the prize, the probability of winning will gradually change, if set to 1 for linear attenuation, 2 for the square attenuation, and so on.







Importjava.util.Date;

Importjava.util.Random;

 

Publicclass Lotterytool {

 

private double factor;

private double probability;

Private Random Rand;

 

Private Lotterytool (double probability, long expiretime, int reduce) {

This.factor = (double) System.currenttimemillis ()/expiretime;

this.probability = probability * MATH.POW (factor, reduce);

This.rand = new Random (System.currenttimemillis ());

}

 

public static Lotterytool getinstance (double probability, longexpiretime,

int reduce) {

return new Lotterytool (probability, expiretime, reduce);

}

 

public boolean islucky (long expected) {

Long token = Generatelong ();

Expected = expected% (int) (1/probability);

if (expected = = token) {

return true;

}

return false;

}





Third. dependence on an uncontrolled physical random number


Understand, hehe, this is now a popular lottery algorithm, absolutely fair, absolutely transparent, absolute wood has a black-out (unless secretly for you to change the lottery number). But the only drawback to this approach is the inability to draw in real time, only after the lottery. It's just a lottery number waiting for God's blessing, amen ...


For example, in the game to beat a boss, will drop one of the following items, and each item has a certain probability: 1. Boots 20% 2. Cloak 25% 3. Accessories 10% 4. Two-handed Sword 5% 5. Coin bag 40% The question now is how to drop an item to the player based on probability.


1. GENERAL algorithm : 

Generate a list, divided into several intervals, such as the length of the list 100,1-20 is the range of boots, 21-45 is the cloak of the interval, and then randomly from 100 to take out a number, to see which interval. Algorithm time complexity: preprocessing O (MN), random number generation O (1), Space complexity O (MN), where n represents the type of goods, M is determined by the lowest probability.


2, the discrete algorithm : 

That is, the above improvements, unexpectedly 1-20 are boots, 21-45 are capes, that the abstract into less than or equal to 20 is the boots, greater than 20 and less than 45 is the cloak, it becomes a few points [20,45,55,60,100], Then also from 1 to 99 randomly take a number r, in order to compare these points, know to find the first larger than R subscript, less space than the general algorithm, you can also use the dichotomy to find R, so that preprocessing O (N), random number generation O (LOGN), Space complexity O (n). Please click to view details: http://www.cnblogs.com/miloyip/archive/2010/04/21/1717109.html


3, Alias Method

Alias method is not very good to understand, the implementation is very clever, recommended first look at this article: http://www.keithschwarz.com/darts-dice-coins/general meaning: The n possibility of assembling into a square (whole), divided into N columns, Each column has a height of 1 and a maximum of two possibilities, the possibility is abstracted to a color, that is, each column has a maximum of two colors, and the nth column must have the nth possibility, where the nth possibility is called the primary color. Imagine throwing a coin that falls on one of the columns and is a color that falls on the column. This gives you two arrays: what is the probability of a record falling on the primary color, the prob array, the color name of the non-primary colors on the other record column, and the alias array, or NULL if the column has only primary colors.

The previous example, in order to facilitate the demonstration to change the score 1. Boots 20%, 1/4 2. Cape 25%, 1/5 3. Jewelry 10%, 1/10 4. Two-hand Sword 5%, 1/20 5. Coin bag 40%-2/5 then each is multiplied by 5 (the height of each column is 1), and then pieced together into a square piece of the principle: each time from a block greater than or equal to 1 a small block, and less than 1 of the block synthesis height of 1

Two arrays can be obtained from the square above: Prob: [3/4, 1] Alias: [4, 4, 0, 1, NULL] (record non-primary color subscript)


Then, according to Prob and alias to get one of the items randomly generated a column of C, and then randomly produce a number r, by comparison with Prob[c], R is larger then return C, and vice versa Alias[c].


Alias Method complexity: preprocessing O (NLOGN), random number generation O (1), Spatial complexity O (2N)


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.