The big turntable is one of the most interesting items in many online activities recently. Let's take a look at the algorithm and example of the winning probability of the big turntable. I hope it will help you. The big turntable is one of the most interesting items in many online activities recently. Let's take a look at the algorithm and example of the winning probability of the big turntable. I hope it will help you.
Script ec (2); script
Recently, I have a project in my work. The APP client has a big turntable lottery. Of course, we have implemented the lottery on our server. The implementation code is as follows:
The Code is as follows: |
|
Header ("Content-type: text/html; charset = UTF-8 "); $ Prize_arr = array ( '0' => array ('id' => 1, 'prize' => 'tablet pc', 'V' => 1 ), '1' => array ('id' => 2, 'prize' => 'digital camera ', 'V' => 5 ), '2' => array ('id' => 3, 'prize' => 'speaker device', 'V' => 10 ), '3' => array ('id' => 4, 'prize' => '4g disks', 'V' => 12 ), '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ), '5' => array ('id' => 6, 'prize' => 'Next time you may be able to get in, ', 'V' => 50 ), ); $ Actor = 100; Foreach ($ prize_arr as $ v ){ $ Arr [$ v ['id'] = $ v ['V']; } Foreach ($ arr as & $ v ){ $ V = $ v * $ actor; } Asort ($ arr ); $ Sum = array_sum ($ arr); // The total probability. $ Rand = mt_rand (1, $ sum ); $ Result = ''; // The winning product id Foreach ($ arr as $ k => $ x) { If ($ rand <= $ x) { $ Result = $ k; Break; } Else { $ Rand-= $ x; } } $ Res ['yes'] = $ prize_arr [$ result-1] ['prize']; // medium Award Print_r ($ res ); |