Tips for using random odds
Author: Yu-Made
(This article is original, reprint please indicate source: http://blog.csdn.net/KyosukeNo1)
In nature, although there are certain objective laws, but there is no absolute possibility and no possibility. Therefore, the use of random probability to describe the occurrence and extinction of things, the most appropriate to reflect the original characteristics of things.
The most general use of stochastic functions is to use% to simply limit the size of random numbers-readers who believe that the random function is often used, are already familiar with the stochastic probability of the random_range of random (), however, in practical applications, Random odds have more complex applications. This article will introduce 2 kinds of random probability use of the tips, in order to stimulate the hope that the reader will be a little discussion.
Tip One: Quick search for cumulative random odds.
Assuming A's probability is 25,b probability is the probability of 35,c is 20, then how to use the simplest method to random, according to set probability to get random number. The following is the implementation code in C + +:
int i; Variables used as loops (nonsense-_-b)
int nprob[] = {25, 35, 20}; Set the probability of each element
int nSum = 0; Variables that store the total number of probabilities
for (i=0; i<sizeof (nprob)/sizeof (int); i++)
NSum + = Nprob[i]; Calculate the total number of probabilities
int nrand = rand ()% NSum; Take a random number within the total probability range
for (i=0; i<sizeof (nprob)/sizeof (int); i++) {
if (Nrand < nprob[i]) {
Break First element hit
} else {
Nrand-= Nprob[i]; The probability of subtracting a random number from an element that was not previously hit
}
}
The steps for this algorithm are simple:
The total number of probabilities is counted first, and then the range of Nrand of random number is taken as the value.
The probability of Nrand and the current element is compared, if the nrand is less than this probability, then hit this element, exit find.
If the Nrand is greater than the probability of the current element, use Nrand to subtract the probability of the element, and then go back to step 2 until all the elements have been found.
The advantage of this algorithm is that the computational amount is small and easy to use in different programming languages.
Tip Two: The odds can be expressed as a quick lookup when the denominator is 2 of the number of squares.
In some cases, the random probability that is currently required to be computed can be expressed as a fraction of this square with a denominator of 2: such as 1/4,3/4,1/16,15/16, and so on, as long as the following formula, can be quickly calculated:
Example 1
The probability of element A is 1/4.
The probability of element B is 3/4.
int nrand = rand () & 0x3;
if (Nrand = = 0)
{
Hit Element A
}
Else
{
Hit element b
}
Example 2
The probability of element A is 1/16.
The probability of element B is 15/16.
int nrand = rand () & 0xF;
if (Nrand = = 0)
{
Hit Element A
}
Else
{
Hit element b
}