# : PHP algorithm that takes random numbers based on probability

Source: Internet
Author: User

Accidentally looking back a year ago (2009-2-10) in PPC and Houlai, Youd discusses the "random number based on probability" algorithm. The problem is this:

Houlai: The probability of a random pumping to a is 0.1,b probability of 0.2,c probability is the probability of a 0.3,d of 0.4, and now the probability of random extraction of a letter by the algorithm

At that time, I had just learned the probability theory, so did not use the online common "use of array initialization, and then fill the content based on probability, and then disturb the array, and then based on a rule to take a certain value in the array" (in fact, the memory will be consumed), and instead of another method:

Horseluke: Think of 1--100 as a line segment. Then the probability is used to cut the line, we have to do is to create a random number, and then see the number corresponding to which line is OK. In this way, the array does not need to be too large, and the same effect can be achieved.

For example, the a letter is 30% probability and the B letter is 70% probability. Then you can divide this 0--100 segment into 2 segments.
A paragraph is 0--30, a letter; the rest belongs to the B field.
Well, the program now randomly extracts 30 of this number, so obviously, the result is a.

|-----------|-----------------------|
0 30 100

But then the implementation of the code, now seems to be a mess. Later, Youd proposed a better algorithm and wrote the PHP code, to make himself ashamed:

Youd: start is from 1,1000 this probability range to filter whether the first number within his probability range, if not, then the probability space, that is, the value of K minus the probability space just that number, in this case is minus 100, that is, the second number is filtered in the range of 1,900. I think it should be easy to understand, so that in the final screening, there will always be a number to meet the requirements (for example, the first three have unfortunately become non-luck Num, then K has been -100-200-300=400, then the last number can meet the requirements anyway.) The equivalent of taking things, the first is not, the second is not, the third one is not, and the last one must be.

The advantage of this algorithm is that it is sufficient to filter on numbers that do not have a probability overlap, and only need to navigate through the array at most. Simple procedure and high efficiency

A year later to look back to this post, really feel that I originally wrote PHP code naïve (of course, now actually level is also very food). Taking advantage of the rest of the current writing paper, the youd algorithm is packaged with functions to be used later.

Do not know the people who discussed together, now how ...

The code is as follows:

The total probability accuracy of probability array
\$proSum = Array_sum (\$PROARR);

foreach (\$proArr as \$key = = \$proCur) {
\$randNum = Mt_rand (1, \$proSum);
if (\$randNum <= \$proCur) {
\$result = \$key;
Break
}else{
\$proSum-= \$proCur;
}
}

return \$result;

}

function Pro_rand_unique_multi (\$PROARR, \$num = 1) {
\$result = Array ();
if (\$num > Count (\$PROARR)) {
Trigger_error (' The stack number of probability Array is GREATER THAN you set! ', 256);
}

while (1) {
if (\$num < 1) {
Break
}
\$curResult = Pro_rand (\$PROARR);
\$result [] = \$curResult;
Reset the total probability accuracy, awaiting the probability theory verification
Unset (\$PROARR [\$curResult]);
\$num-= 1;
}

return \$result;

}

Test code for the above function:

Pro_rand:

\$PROARR = Array (10,20,30,40);
Mt_srand (Time ());

\$distribute = Array ();
for (\$k = 1; \$k <=, \$k + +) {

\$result = Pro_rand (\$PROARR);
if (!isset (\$distribute [\$result]) {
\$distribute [\$result] = 1;
}else{
\$distribute [\$result] + = 1;
}

}

Ksort (\$distribute);
Var_export (\$distribute);

Pro_rand_unique_multi:

\$PROARR = Array (10,20,30,40);
Mt_srand (Time ());

\$distribute = Array ();
for (\$k = 1; \$k <=, \$k + +) {

}

Ksort (\$distribute);
Var_export (\$distribute);

Finish

: PHP algorithm that takes random numbers based on probability

Related Keywords:

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.