Home php教程 PHP源码 抽奖概率 PHP

抽奖概率 PHP

May 26, 2016 am 08:21 AM

抽奖概率

<?php 

function get_rand($proArr) { 
    $result = &#39;&#39;; 

    //概率数组的总概率精度
    $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; 
}

$prize_arr = array( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板电脑&#39;,&#39;v&#39;=>0), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;数码相机&#39;,&#39;v&#39;=>0), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱设备&#39;,&#39;v&#39;=>0), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G优盘&#39;,&#39;v&#39;=>0), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;10Q币&#39;,&#39;v&#39;=>1), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次没准就能中哦&#39;,&#39;v&#39;=>100), 
);  


//code here eg:unset($prize_arr[&#39;0&#39;])
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 

$rid = get_rand($arr); //根据概率获取奖项id

$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中奖项
//将中奖项从数组中剔除,剩下未中奖项,如果是数据库验证,这里可以省掉
unset($prize_arr[$rid-1]); 
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){ 
    $pr[] = $prize_arr[$i][&#39;prize&#39;]; 
} 
$res[&#39;no&#39;] = $pr; 
echo "<pre class="brush:php;toolbar:false">";
print_r($res);
// echo json_encode($res);
Copy after login
Copy after login

2. [代码]抽奖概率

<?php 

function get_rand($proArr) { 
    $result = &#39;&#39;; 

    //概率数组的总概率精度
    $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; 
}

$prize_arr = array( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板电脑&#39;,&#39;v&#39;=>0), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;数码相机&#39;,&#39;v&#39;=>0), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱设备&#39;,&#39;v&#39;=>0), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G优盘&#39;,&#39;v&#39;=>0), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;10Q币&#39;,&#39;v&#39;=>1), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次没准就能中哦&#39;,&#39;v&#39;=>100), 
);  


//code here eg:unset($prize_arr[&#39;0&#39;])
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 

$rid = get_rand($arr); //根据概率获取奖项id

$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中奖项
//将中奖项从数组中剔除,剩下未中奖项,如果是数据库验证,这里可以省掉
unset($prize_arr[$rid-1]); 
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){ 
    $pr[] = $prize_arr[$i][&#39;prize&#39;]; 
} 
$res[&#39;no&#39;] = $pr; 
echo "<pre class="brush:php;toolbar:false">";
print_r($res);
// echo json_encode($res);
Copy after login
Copy after login


 以上就是抽奖概率的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Quantile regression for time series probabilistic forecasting Quantile regression for time series probabilistic forecasting May 07, 2024 pm 05:04 PM

Do not change the meaning of the original content, fine-tune the content, rewrite the content, and do not continue. "Quantile regression meets this need, providing prediction intervals with quantified chances. It is a statistical technique used to model the relationship between a predictor variable and a response variable, especially when the conditional distribution of the response variable is of interest When. Unlike traditional regression methods, quantile regression focuses on estimating the conditional magnitude of the response variable rather than the conditional mean. "Figure (A): Quantile regression Quantile regression is an estimate. A modeling method for the linear relationship between a set of regressors X and the quantiles of the explained variables Y. The existing regression model is actually a method to study the relationship between the explained variable and the explanatory variable. They focus on the relationship between explanatory variables and explained variables

How to make an excel lottery applet How to make an excel lottery applet Mar 20, 2024 am 11:40 AM

In daily work, we encounter a lot of things that require drawing lots. The traditional method is to randomly draw numbers using paper numbers. With the development of electronic software, we can use computers to draw lots. The lesson I want to share with you today is How to make an excel lottery applet. 1. First, we open the Excel software and open the table we prepared. The table must contain our names. 2. Then we merge the cells on the right, fill in black who is lucky tonight, and merge the cells below and fill in red, as shown in the figure below. 3. Then we enter the randbetween function in the red area and set the first line to 2 and the last line to 7, as shown in the figure below. 4. Then we enter ind in front

Detailed explanation of the design and implementation of PHP lottery system Detailed explanation of the design and implementation of PHP lottery system Mar 01, 2024 am 09:51 AM

Detailed explanation of the design and implementation of PHP lottery system 1. Overview Lottery is a marketing tool used by many websites and applications. Through lottery, it can attract users to participate in activities, increase user interactivity and improve user stickiness. In this article, we will introduce in detail how to use PHP language to design and implement a simple lottery system. By studying this article, readers will understand the construction principles and specific code implementation of the lottery system. 2. System Design Before designing a lottery system, we first need to determine the functional requirements and processes of the system. one

How to use Vue to implement lottery wheel special effects How to use Vue to implement lottery wheel special effects Sep 19, 2023 am 10:18 AM

How to use Vue to implement lottery wheel special effects. As a popular marketing tool, lottery activities often appear in various activities. In order to increase fun and interactivity, we can use the Vue framework to implement a lottery wheel special effect. This article will introduce how to use Vue to achieve this special effect and give specific code examples. First, we need to create a Vue instance to control the status and animation of the turntable. In this example, we need to define the initial state of the wheel, including the prize list, whether the wheel is rotating,

Steps and techniques for developing lottery functions using PHP Steps and techniques for developing lottery functions using PHP Feb 29, 2024 pm 04:42 PM

Title: Steps and techniques for developing lottery functions using PHP. In website development, lottery functions are often used to increase user interactivity and attraction. Using PHP language to develop lottery functions is a common way. In this article, we will introduce how to use PHP to develop a simple lottery function and give specific code examples. Step 1: Prepare the database First, we need to create a MySQL database to store relevant data of the lottery, including prize information, participating user information, etc. You can use the following SQL command to create a

How to increase the winning rate in Douyin lottery? What are the techniques? How to increase the winning rate in Douyin lottery? What are the techniques? Mar 07, 2024 pm 02:43 PM

In today's prosperous era of social media, Douyin short videos have become an indispensable part of people's lives. The lottery on Douyin also attracted the attention and participation of countless users. However, many people often encounter a problem: How to improve their winning rate in Douyin lottery? This article will summarize several tips for increasing your winning rate to help you get more good luck in the Douyin lottery. 1. Read the lottery rules carefully. To increase the winning rate, we cannot ignore the lottery rules. Each lottery will have detailed rules, including participation conditions, lottery time and method, etc. Before participating, be sure to read the rules carefully and make sure you meet all conditions. Sometimes, you can also discover some hidden opportunities by carefully reading the rules, thereby increasing your chances of success.

The long sword cuts through the thunder in anger! 'Dragon Warrior' PC game will soon launch a new thunder and lightning profession - the fierce sword 'Thunder' The long sword cuts through the thunder in anger! 'Dragon Warrior' PC game will soon launch a new thunder and lightning profession - the fierce sword 'Thunder' Mar 01, 2024 pm 05:07 PM

The 2024 new expansion pack of "Dragon Warrior" "Fighting Sword and Thunder" will be officially launched on March 8! The thunderous thunder will not kill Wuming! The new and exciting career "Thunder" is coming! This new profession can rely on the power of thunder and lightning to turn thunder into blades, and use its unique thunder sword energy to divide the battlefield in battle. It has very powerful group burst damage and endurance, and will be used in team battles with its excellent displacement mechanism. Unparalleled strategic effect! The background story of the new profession Jing Lei is a female hero who is good at swordsmanship and possesses the mysterious ability connected with thunder and lightning. Able to integrate thunder and lightning spells and melee sword skills, and the sword swings are accompanied by lightning and thunder. From move to move, her sword skills are as fast and unpredictable as thunder. Each sword carries the power of thunder and lightning, making the enemy flee. Ran

How to implement lottery in php How to implement lottery in php Dec 19, 2022 am 10:10 AM

How to implement lottery in PHP: 1. Create a PHP sample file; 2. Define the prizes owned by the prize pool; 3. Conduct random lottery through the "shuffle($prize);" method; 4. Use "print_r($prize[0]) ;" Just print the lottery results.

See all articles