Home PHP Libraries Other libraries PHP function class for winning probability algorithm
PHP function class for winning probability algorithm
<?php
function get_rand($proArr) {
    $result = '';
    //概率数组的总概率精度
    $proSum = array_sum($proArr);
    //概率数组循环
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $proCur) {
            $result = $key;
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
    return $result;
}

The prize array is a two-dimensional array that records all the prize information of this lottery, where id represents the winning level, prize represents the prize, and v represents the probability of winning. Note that v must be an integer. You can set v of the corresponding award to 0, which means that the probability of winning the award is 0. The sum of v in the array (base). The larger the base, the more accurate the probability can be reflected. . In this example, the sum of v is 100, then the probability of winning for the tablet is 1%. If the sum of v is 10,000, the probability of winning is one in ten thousand. $proArr is a preset array. Assume that the array is: array(100,200,300,400). It starts by screening the first number from the probability range of 1,1000 to see if it is within its occurrence probability range. If not, it will The probability space is the probability space of the value of k minus the number just now. In this case, it is minus 100, which means that the second number is filtered within the range of 1,900. In this way, until the end of the screening, there will always be a number that meets the requirements. It's like touching something in a box. If the first one isn't there, the second one isn't there, and the third one isn't there, then the last one must be. This algorithm is simple and very efficient. The key is that this algorithm has been applied in our previous projects, especially in projects with large amounts of data. The efficiency is very good.


Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

Winning probability algorithm (php can be used for scratch cards, big wheel and other lottery algorithms) Winning probability algorithm (php can be used for scratch cards, big wheel and other lottery algorithms)

06 Jul 2016

PHP winning probability algorithm, which can be used for scratch card, big wheel and other lottery algorithms. The usage is very simple, there are detailed comments in the code, you can understand it at a glance? php /* * Classic probability algorithm, * $PRoArr is a preset array, * Assume the array is: array(100,200,300, 400), * The beginning is Screen the first number from the probability range of 1,1000 to see if it is

How Do I Link Static Libraries That Depend on Other Static Libraries? How Do I Link Static Libraries That Depend on Other Static Libraries?

13 Dec 2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

How to Silence TensorFlow\'s Debugging Output? How to Silence TensorFlow\'s Debugging Output?

28 Oct 2024

Suppression of Tensorflow Debugging OutputTensorflow prints extensive information about loaded libraries, found devices, and other debugging data...

How Does jQuery Simplify DOM Manipulation for Web Developers? How Does jQuery Simplify DOM Manipulation for Web Developers?

03 Jan 2025

Overflow: Hidden and Expansion of HeightjQuery distinguishes itself from other JavaScript libraries through its cross-platform compatibility and...

Which native Java image processing library is right for you? Which native Java image processing library is right for you?

30 Oct 2024

Native Java Image Processing Libraries for High-Quality ResultsAs you have encountered limitations with ImageMagick and JAI, let's explore other...

How to Execute Command Line Binaries in Node.js? How to Execute Command Line Binaries in Node.js?

27 Dec 2024

Executing Command Line Binaries in Node.jsExecuting third-party binaries is an essential task when porting CLI libraries from other languages to...

See all articles