Cocos2dx lottery page and prize winning probability design (2): cocos2dx Lucky Draw
**************************************** ************************************
Time:
Author: Sharing_Li
Reprinted with the source: http://blog.csdn.net/sharing_li/article/details/43405569
**************************************** ************************************
In the previous blog, we explained the design of the lottery interface, which focuses on EllipseBy, the classification class of the lower-edge elliptical motion. This article describes how to design the lottery probability.
There are two ways to draw a lottery: the first is to turn the turntable first and then to whom it is. The second is to calculate the prize you have drawn and then transfer it to the corresponding Prize (I wiped it, isn't it a dark box operation !!).
Let's take a look at the first lucky draw method:
This turntable has three prizes. Because the three slices have the same area, the probability of winning is the same.1/3. If we want to change the lottery probability of each type of prize, we can change the slice area, for example, the probability is1/4, The Slice area is the whole circle1/4. For example:
If we want to dynamically change the probability of a lottery, one way is to change the base map of the turntable, but obviously this is not good. Let's think about how hackers operate in a dark box in real life, such as taking an Iron Stone and putting it behind the turntable to influence the position of the turntable pointer. This method can be used for reference (I am not a profiteer ~), For example, if we set an obstacle factor for each sector on the turntable, the higher the probability of winning the award, the larger the obstacle factor. Therefore, when the turntable is turned, the pointer will subtract the drive factor of this sector from the speed of the turntable every time it enters a sector area. In this way, we can dynamically change the probability of a lucky draw, but this "dark box operation" is not highly accurate and difficult to control. We will discuss the second method in detail later. Let's take a look at how to determine who is being drawn?
There are two methods:
The first is to determine the position of the needle of the rotating pointer in which the slice area, that is, whether the point and irregular shape collision, can refer to this article: http://www.2cto.com/kf/201401/272331.html
The second is based on the angle of each slice. The angle of the entire circle is 360. If there are three slice slices in total, the angle range of each slice is[0,100],[100,280],[280,360]. We can use getRotation to get the degree of rotation of the turntable, and then perform the modulo operation, % 360. Then we can see the result within the range of the three slices.
Although the method mentioned above is more practical, it is not particularly suitable for game development. We hope to control the lottery probability more accurately, you can also change the probability of a lottery dynamically based on the number of Lottery draws. Then let's talk about the second method of lottery, that is, calculate and win the prize. In this method, all slice areas are equal.
As a game developer, do we want to share the lottery with everyone or do we have everything in our hands? If the prize contains the best equipment, especially the quantity is very scarce, what should I do if the player is lucky? In games, Lottery draws are generally free for the first time every day. virtual coins are consumed for subsequent Lottery draws. As long as virtual coins are consumed, we need to carefully design them, after all, it is related to the revenue of our game developers.Here are two simple methods.
First, if there are four prizes a, B, C, and D, we specify the number of Lottery draws for each prize N, N means at least N times, you can only get the prize, and then define another M to count the number of times the player has smoked. For example, the N values of the four prizes are Na: 1, Nb: 2, Nc: 10, Nd: 50, and the initial M value is 1, m> Na, must be drawn in A, the second pumping, M ++, M> Nb, must be in B; Continue to draw, M ++, Nb <M = 4 <Nc, among the prizes whose values are less than M, that is, Na and Nb are randomly selected. Until M = 11> Nc, select C, then continue pumping, select one randomly in Na, Nb, and Nc, and so on. In addition, if the N values of some prizes are the same, they will be randomly selected. When the value of M is greater than the value of N for all prizes, some rare and valuable prizes will become more likely to be drawn. So we can do this. For example, we regard the prize D as a rare prize. After the first draw, we change the N value corresponding to the prize D, N = N + N/2. Of course, if you have a black heart, set N = N * 3 in this way (it is estimated that players will fight you together ...). After confirming the prize, calculate the degree of rotation, and the pointer stays in the sector of the prize. Finally, callRotateByYou can.
The above method is more accurate and flexible. Let's take a look at the second method.
Assume that there are five prizes in total: A, B, C, D, and E. The winning rates of each prize are: Ra: 80%, Rb: 30%, Rc: 50%, Rd: 30%, Re: 10%, we define an int type base, the value can be random, but it should be appropriate. Here we set it to 100, and then we can get the winning range of each prize: A: [80,110], B: [110,160], C: [], D: [160,190], E: [190,200], and then we randomly take a number between the range [0,200] to see which range the number is in, then the corresponding prize will be drawn. Then let's take a look at how to change the probability through the number of players' Lottery draws. We define A coefficient p. The value can be obtained at will. Here we take 2. Then we get the variable value V = the number of Lottery draws M * p. Then the original lottery probability will be changed to:: [80,110 + V], B: [110,160 + V], C: [160,190 + V], D: [190 + V], E: [200 + V,], in this case, there will be overlapping areas. For example, if the random number I obtain is equal to 85 and A and B are in the range, then A random value can be obtained. Similarly, after determining the prize to be drawn, calculate the angle of rotation and call RotateBy.
Here, the winning method has been introduced. Note that the random number acquisition algorithm must be closer to the natural random number. Here we will give a general introduction to some random algorithms, as shown in the Code:
// The first type // the random effect is normal. Every time you enter the game, the values are the same m_randData = CCRANDOM_0_1 () * 200; log ("1st -------------> % f ", m_randData); // The second type // the random effect is not good. Although the values are different each time you enter the game, the random number generated increases progressively, in addition, the number of adjacent numbers does not change much between srand (unsigned (time (NULL); m_randData = rand () % 200; log ("2rd -------------> % f", m_randData ); // The third type // the random effect is good. The value of each game entry is different, and the random number generated is irregular. The adjacent two numbers change greatly in timeval TV; cocos2d :: gettimeofday (& TV, NULL); unsigned long int seed = TV. TV _sec * 1000 + TV. TV _u SEC/1000 ; Srand (seed); m_randData = rand () % 200; log ("3nd -------------> % f", m_randData); // The fourth type // C ++ 11 features, to include the header file <random>, the random effect is good. Std: uniform_real_distribution <double> u (0,200); std: default_random_engine e (rand (); for (int I = 0; I <5; I ++) {log ("4th -------------> % f", u (e ));}
We can also implement Random Algorithms by ourselves. We don't have to use APIs to provide them. I will not discuss them in depth here. There are too many contents and I will write them later.
Now, the entire article has been explained. Please leave a message.