


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

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ??of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ??of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出php提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出而不是在空白頁(yè)彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁(yè)彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.
