


PHP lottery probability algorithm (scratch card, big carousel), lottery scratch card_PHP tutorial
Jul 12, 2016 am 08:49 AMphp lottery probability algorithm (scratch card, big carousel), lottery scratch card
The example in this article shares the php winning probability algorithm, which can be used for scratch cards, large The lottery algorithm such as the turntable is very simple to use. There are detailed comments in the code for your reference. The specific content is as follows
<?php /* * 經(jīng)典的概率算法, * $proArr是一個預(yù)先設(shè)置的數(shù)組, * 假設(shè)數(shù)組為:array(100,200,300,400), * 開始是從1,1000 這個概率范圍內(nèi)篩選第一個數(shù)是否在他的出現(xiàn)概率范圍之內(nèi), * 如果不在,則將概率空間,也就是k的值減去剛剛的那個數(shù)字的概率空間, * 在本例當(dāng)中就是減去100,也就是說第二個數(shù)是在1,900這個范圍內(nèi)篩選的。 * 這樣 篩選到最終,總會有一個數(shù)滿足要求。 * 就相當(dāng)于去一個箱子里摸東西, * 第一個不是,第二個不是,第三個還不是,那最后一個一定是。 * 這個算法簡單,而且效率非常高, * 這個算法在大數(shù)據(jù)量的項目中效率非常棒。 */ function get_rand($proArr) { $result = ''; //概率數(shù)組的總概率精度 $proSum = array_sum($proArr); //概率數(shù)組循環(huán) foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset ($proArr); return $result; } /* * 獎項數(shù)組 * 是一個二維數(shù)組,記錄了所有本次抽獎的獎項信息, * 其中id表示中獎等級,prize表示獎品,v表示中獎概率。 * 注意其中的v必須為整數(shù),你可以將對應(yīng)的 獎項的v設(shè)置成0,即意味著該獎項抽中的幾率是0, * 數(shù)組中v的總和(基數(shù)),基數(shù)越大越能體現(xiàn)概率的準確性。 * 本例中v的總和為100,那么平板電腦對應(yīng)的 中獎概率就是1%, * 如果v的總和是10000,那中獎概率就是萬分之一了。 * */ $prize_arr = array( '0' => array('id'=>1,'prize'=>'平板電腦','v'=>1), '1' => array('id'=>2,'prize'=>'數(shù)碼相機','v'=>5), '2' => array('id'=>3,'prize'=>'音箱設(shè)備','v'=>10), '3' => array('id'=>4,'prize'=>'4G優(yōu)盤','v'=>12), '4' => array('id'=>5,'prize'=>'10Q幣','v'=>22), '5' => array('id'=>6,'prize'=>'下次沒準就能中哦','v'=>50), ); /* * 每次前端頁面的請求,PHP循環(huán)獎項設(shè)置數(shù)組, * 通過概率計算函數(shù)get_rand獲取抽中的獎項id。 * 將中獎獎品保存在數(shù)組$res['yes']中, * 而剩下的未中獎的信息保存在$res['no']中, * 最后輸出json個數(shù)數(shù)據(jù)給前端頁面。 */ foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $rid = get_rand($arr); //根據(jù)概率獲取獎項id $res['yes'] = $prize_arr[$rid-1]['prize']; //中獎項 unset($prize_arr[$rid-1]); //將中獎項從數(shù)組中剔除,剩下未中獎項 shuffle($prize_arr); //打亂數(shù)組順序 for($i=0;$i<count($prize_arr);$i++){ $pr[] = $prize_arr[$i]['prize']; } $res['no'] = $pr; print_r($res); ?>
The above is the entire content of the PHP lottery probability algorithm. I hope it will be helpful to everyone in learning PHP programming. I also hope that everyone will support Bangkejia.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

exit() is a function in PHP that is used to terminate script execution immediately. Common uses include: 1. Terminate the script in advance when an exception is detected, such as the file does not exist or verification fails; 2. Output intermediate results during debugging and stop execution; 3. Call exit() after redirecting in conjunction with header() to prevent subsequent code execution; In addition, exit() can accept string parameters as output content or integers as status code, and its alias is die().

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

There are two ways to create an array in PHP: use the array() function or use brackets []. 1. Using the array() function is a traditional way, with good compatibility. Define index arrays such as $fruits=array("apple","banana","orange"), and associative arrays such as $user=array("name"=>"John","age"=>25); 2. Using [] is a simpler way to support since PHP5.4, such as $color

When you encounter the prompt "This operation requires escalation of permissions", it means that you need administrator permissions to continue. Solutions include: 1. Right-click the "Run as Administrator" program or set the shortcut to always run as an administrator; 2. Check whether the current account is an administrator account, if not, switch or request administrator assistance; 3. Use administrator permissions to open a command prompt or PowerShell to execute relevant commands; 4. Bypass the restrictions by obtaining file ownership or modifying the registry when necessary, but such operations need to be cautious and fully understand the risks. Confirm permission identity and try the above methods usually solve the problem.

The way to process raw POST data in PHP is to use $rawData=file_get_contents('php://input'), which is suitable for receiving JSON, XML, or other custom format data. 1.php://input is a read-only stream, which is only valid in POST requests; 2. Common problems include server configuration or middleware reading input streams, which makes it impossible to obtain data; 3. Application scenarios include receiving front-end fetch requests, third-party service callbacks, and building RESTfulAPIs; 4. The difference from $_POST is that $_POST automatically parses standard form data, while the original data is suitable for non-standard formats and allows manual parsing; 5. Ordinary HTM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp
