php Several commonly used probabilistic algorithms (sweepstakes, advertisements preferred)

Do site classes sometimes get hold of what activities to allow users to participate, not only to attract user registration, user activity and improve the site. At the same time participating users will get some prizes, 100% winning, but also a certain probability of winning, such as in a large ipad, iphone5, small in Q coins or something. So we have in the program is bound to design algorithm, according to a certain probability allows users to get a prize. First look at two probabilistic algorithms function.

 

A algorithm

/ * * 
 * Probability Full 
 * 
 * @param Array Array P $ ( 'A' => 0.5, 'B' => 0.2, 'C' => 0.4) 
 * @return String returned above the array Key 
 * / 
function Random ( $ PS ) {
     static  $ ARR = Array ();
     $ Key = MD5 ( the serialize ( $ PS )); 
 
    IF (! isset ( $ ARR [ $ Key {]))
         $ max = array_sum ( $ PS );
         the foreach ( PS $  AS  $ K => $ V) {
            $v = $v / $max * 10000;
            for ($i=0; $i<$v; $i++) $arr[$key][] = $k;
        }
    }
    return $arr[$key][mt_rand(0,count($arr[$key])-1)];
} 

Algorithms two

function get_rand ( $ proArr ) {
     $ Result = '' ; 
 
    // total accuracy probability probability array 
    $ PROSUM = array_sum ( $ proArr ); 
 
    // probability array cycle 
    the foreach ( $ proArr  AS  $ Key => $ proCur ) {
         $ randnum = the mt_rand (. 1, $ PROSUM );
         IF ( $ randnum <= $ proCur ) {
             $ Result = $ Key ;
             BREAK ; 
        } the else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
 
    return $result;
}

The second is a classic algorithm of probabilistic algorithms, $ proArr is an array of pre-set, assuming that the array is: array (100,200,300,400), begin screening this probability range from 1,1000 whether the number in his first appearance probability within range, if not, will reduce the probability of an empty, that is, the value of k by subtracting the probability space is just that number, which in this embodiment 100 is subtracted, i.e. the second number in 1,900 within the scope of screening. Such screening to the final, there will always be a number to meet the requirements. It is equivalent to touch something in a box, not the first, not the second, not the third, that last a certain yes. This algorithm is simple, but the efficiency is very high, the key is the algorithm has been used in our previous projects, especially large amount of data items in great efficiency.

Next we configure awards by PHP.

$ prize_arr = Array (
     '0' => Array ( 'ID' =>. 1, 'Prize' => 'tablet', 'V' =>. 1), 
    '. 1' => Array ( 'ID' => 2 , 'prize' => 'digital camera', 'V' =>. 5), 
    '2' => Array ( 'ID' =>. 3, 'prize' => 'speaker apparatus', 'v' => 10 ) , 
    '. 3' => Array ( 'ID' =>. 4, 'Prize' => '4G USB', 'V' => 12 is), 
    '. 4' => Array ( 'ID' =>. 5, 'Prize' => '10Q credits',' V '=> 22 is), 
    '. 5 '=> Array (' ID '=>. 6,' Prize '=>'Probably the next can Oh ',' V '=> 50), 
);

$ Prize_arr is a two-dimensional array, which records all information in this sweepstakes awards, where id is the winning level, prize represents the prize, v represents the probability of winning. Note where v must be an integer, you can award the corresponding v is set to 0, which means that the probability of the prize draw is 0, the sum of the array v (base), the greater the base to reflect the probability of accuracy . The sum of v in the present embodiment is 100, then the tablet corresponding to the probability of winning is 1%, if the sum of v is 10000, that is one ten thousandth of the probability of winning.

Front page of each request, PHP loop array disposed awards by prize probability calculation function acquires id get_rand drawn in. The winning prize stored in the array $ res [ 'yes'], whereas the rest of the information is not stored in the winning $ res [ 'no'], the final output data to json front page number.

// If the data is on winning the database, where you need to determine the number of winning 
// in 1,2,3 prize, if the maximum number is unset the corresponding award, to avoid duplication in the award 
// code here EG: the unset ($ prize_arr [ '0']) 
the foreach ( $ prize_arr  AS  $ Key => $ Val ) {
     $ ARR [ $ Val [ 'ID']] = $ Val [ 'V' ]; 
} 
 
$ RID = get_rand ( $ ARR ); // Get the probability awards id 
 
$ RES [ 'Yes'] = $ prize_arr [ $ RID -1] [ 'prize']; // the prize 
// remove the prize from the array, the remaining miss award, if the verification database, can be dispensed with where 
the unset ( $ prize_arr [ $ RID -1]);
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){
    $pr[] = $prize_arr[$i]['prize'];
}
$res['no'] = $pr;
echo json_encode($res); 

Why do not prize draw it?

In many similar sweepstakes, participants tend to smoke less than awards, I give an example from the perspective of the program you see, if I were the organizers of the raffle, I set up six awards, winning prizes for each different probability, if the prize is a limousine, but I set up a prize which probability is 0, which means what? This means that no matter how those who participate in the lottery draw, never get this limousine. When turning the rest of the organizers of each block, participants will find perhaps the first prize in the lottery just a number next to the box, blame their own bad luck. It really is bad luck? In fact, when participants turning the box in the award procedure has been decided, and flip the box to see other awards see just a smoke bomb, confuse the audience and participants. I think reading this article, you might know that the flap lottery television program in the transmission of the disease, then you might probably not going to machine the selected color ball.

 

Original: https://www.cnblogs.com/qhorse/p/4838296.html

 

A algorithm

/ * * 
 * Probability Full 
 * 
 * @param Array Array P $ ( 'A' => 0.5, 'B' => 0.2, 'C' => 0.4) 
 * @return String returned above the array Key 
 * / 
function Random ( $ PS ) {
     static  $ ARR = Array ();
     $ Key = MD5 ( the serialize ( $ PS )); 
 
    IF (! isset ( $ ARR [ $ Key {]))
         $ max = array_sum ( $ PS );
         the foreach ( PS $  AS  $ K => $ V) {
            $v = $v / $max * 10000;
            for ($i=0; $i<$v; $i++) $arr[$key][] = $k;
        }
    }
    return $arr[$key][mt_rand(0,count($arr[$key])-1)];
} 

Algorithms two

function get_rand ( $ proArr ) {
     $ Result = '' ; 
 
    // total accuracy probability probability array 
    $ PROSUM = array_sum ( $ proArr ); 
 
    // probability array cycle 
    the foreach ( $ proArr  AS  $ Key => $ proCur ) {
         $ randnum = the mt_rand (. 1, $ PROSUM );
         IF ( $ randnum <= $ proCur ) {
             $ Result = $ Key ;
             BREAK ; 
        } the else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
 
    return $result;
}

The second is a classic algorithm of probabilistic algorithms, $ proArr is an array of pre-set, assuming that the array is: array (100,200,300,400), begin screening this probability range from 1,1000 whether the number in his first appearance probability within range, if not, will reduce the probability of an empty, that is, the value of k by subtracting the probability space is just that number, which in this embodiment 100 is subtracted, i.e. the second number in 1,900 within the scope of screening. Such screening to the final, there will always be a number to meet the requirements. It is equivalent to touch something in a box, not the first, not the second, not the third, that last a certain yes. This algorithm is simple, but the efficiency is very high, the key is the algorithm has been used in our previous projects, especially large amount of data items in great efficiency.

Next we configure awards by PHP.

$ prize_arr = Array (
     '0' => Array ( 'ID' =>. 1, 'Prize' => 'tablet', 'V' =>. 1), 
    '. 1' => Array ( 'ID' => 2 , 'prize' => 'digital camera', 'V' =>. 5), 
    '2' => Array ( 'ID' =>. 3, 'prize' => 'speaker apparatus', 'v' => 10 ) , 
    '. 3' => Array ( 'ID' =>. 4, 'Prize' => '4G USB', 'V' => 12 is), 
    '. 4' => Array ( 'ID' =>. 5, 'Prize' => '10Q credits',' V '=> 22 is), 
    '. 5 '=> Array (' ID '=>. 6,' Prize '=>'Probably the next can Oh ',' V '=> 50), 
);

$ Prize_arr is a two-dimensional array, which records all information in this sweepstakes awards, where id is the winning level, prize represents the prize, v represents the probability of winning. Note where v must be an integer, you can award the corresponding v is set to 0, which means that the probability of the prize draw is 0, the sum of the array v (base), the greater the base to reflect the probability of accuracy . The sum of v in the present embodiment is 100, then the tablet corresponding to the probability of winning is 1%, if the sum of v is 10000, that is one ten thousandth of the probability of winning.

Front page of each request, PHP loop array disposed awards by prize probability calculation function acquires id get_rand drawn in. The winning prize stored in the array $ res [ 'yes'], whereas the rest of the information is not stored in the winning $ res [ 'no'], the final output data to json front page number.

// If the data is on winning the database, where you need to determine the number of winning 
// in 1,2,3 prize, if the maximum number is unset the corresponding award, to avoid duplication in the award 
// code here EG: the unset ($ prize_arr [ '0']) 
the foreach ( $ prize_arr  AS  $ Key => $ Val ) {
     $ ARR [ $ Val [ 'ID']] = $ Val [ 'V' ]; 
} 
 
$ RID = get_rand ( $ ARR ); // Get the probability awards id 
 
$ RES [ 'Yes'] = $ prize_arr [ $ RID -1] [ 'prize']; // the prize 
// remove the prize from the array, the remaining miss award, if the verification database, can be dispensed with where 
the unset ( $ prize_arr [ $ RID -1]);
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){
    $pr[] = $prize_arr[$i]['prize'];
}
$res['no'] = $pr;
echo json_encode($res); 

Why do not prize draw it?

In many similar sweepstakes, participants tend to smoke less than awards, I give an example from the perspective of the program you see, if I were the organizers of the raffle, I set up six awards, winning prizes for each different probability, if the prize is a limousine, but I set up a prize which probability is 0, which means what? This means that no matter how those who participate in the lottery draw, never get this limousine. When turning the rest of the organizers of each block, participants will find perhaps the first prize in the lottery just a number next to the box, blame their own bad luck. It really is bad luck? In fact, when participants turning the box in the award procedure has been decided, and flip the box to see other awards see just a smoke bomb, confuse the audience and participants. I think reading this article, you might know that the flap lottery television program in the transmission of the disease, then you might probably not going to machine the selected color ball.

 

Original: https://www.cnblogs.com/qhorse/p/4838296.html

Guess you like

Origin www.cnblogs.com/ivy-zheng/p/11272679.html