


Summary of PHP probability calculation functions, php probability function_PHP tutorial
PHP概率计算函数汇总,php概率函数
其实发这篇博感觉并没有什么用,太简单了,会的人不屑看,不会的人自已动动脑子也想到了。但是看着自已的博客已经这么久没更,真心疼~。粗略算下一篇只有代码的水文,会占用OSC至少十几KB的数据库空间呢,但是,一想到乱弹里的然并卵,也就释然了。
<?php /** * 概率计算类 * 可用于抽奖等 */ class Probability { /** * 概率统计数据 * thing => chance */ var $data = array(); var $chance_count = 0; function __construct($initdata = array()){ if(!empty($initdata)){ $this->data = $initdata; foreach($initdata as $d){ $this->chance_count += $d['num']; } } } function addData($name, $chance){ $this->data[]=array('name'=>$name, 'num'=>$chance); $this->chance_count += $chance; } function getOne(){ $index = rand(0, $this->chance_count); foreach($this->data as $d){ $index = $index-$d['num']; if($index<=0){ return $d['name']; } } return ''; } } /** * 使用示例 */ $pro=new Probability(); $pro->addData('iphone',10); $pro->addData('watch',30); $pro->addData('$18',50); $pro->addData('thank you',10); $pro->addData('super big',1); for($i=0;$i<100;$i++){ echo $pro->getOne()."\n"; }
这是一个很经典的概率算法函数:
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; }
假设:我们有这样一个数组:a奖概率20%,b奖概率30%,c奖概率50%
$prize_arr =array('a'=>20,'b'=>30,'c'=>50);
模拟函数执行过程:
总概率精度为20+30+50=100
第一次数组循环,$procur=20
假设抽取的随机数rand(1,100),假设抽到$randNum=55
if判断-------
如果$randNum
否则进入下一循环,总概率精度变为100-20=80
第二次数组循环,$procur=30
假设抽取的随机数rand(1,80),假设抽到$randNum=33
if判断---------
如果$randNum
否则进入下一循环,总概率精度变为80-30=50
第三次数组循环,$prosur=50;
假设抽取的随机数rand(1,50),不管怎么抽,随机数都会
那么得出result=c;
因为样本没有改变,虽然可能抽取的随机数不止一个,但是概率是不变的。
或者也可以这样:
function get_rand($arr) { $pro_sum=array_sum($arr); $rand_num=mt_rand(1,$pro_sum); $tmp_num=0; foreach($arr as $k=>$val) { if($rand_num<=$val+$tmp_num) { $n=$k; break; }else { $tmp_num+=$val; } } return $n; }
在给大家分享一个抽奖的概率算法
/* * 经典的概率算法, * $proArr是一个预先设置的数组, * 假设数组为:array(100,200,300,400), * 开始是从1,1000 这个概率范围内筛选第一个数是否在他的出现概率范围之内, * 如果不在,则将概率空间,也就是k的值减去刚刚的那个数字的概率空间, * 在本例当中就是减去100,也就是说第二个数是在1,900这个范围内筛选的。 * 这样 筛选到最终,总会有一个数满足要求。 * 就相当于去一个箱子里摸东西, * 第一个不是,第二个不是,第三个还不是,那最后一个一定是。 * 这个算法简单,而且效率非常 高, * 关键是这个算法已在我们以前的项目中有应用,尤其是大数据量的项目中效率非常棒。 */ 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; } /* * 奖项数组 * 是一个二维数组,记录了所有本次抽奖的奖项信息, * 其中id表示中奖等级,prize表示奖品,v表示中奖概率。 * 注意其中的v必须为整数,你可以将对应的 奖项的v设置成0,即意味着该奖项抽中的几率是0, * 数组中v的总和(基数),基数越大越能体现概率的准确性。 * 本例中v的总和为100,那么平板电脑对应的 中奖概率就是1%, * 如果v的总和是10000,那中奖概率就是万分之一了。 * */ $prize_arr = array( '0' => array('id'=>1,'prize'=>'平板电脑','v'=>1), '1' => array('id'=>2,'prize'=>'数码相机','v'=>5), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>10), '3' => array('id'=>4,'prize'=>'4G优盘','v'=>12), '4' => array('id'=>5,'prize'=>'10Q币','v'=>22), '5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50), ); /* * 每次前端页面的请求,PHP循环奖项设置数组, * 通过概率计算函数get_rand获取抽中的奖项id。 * 将中奖奖品保存在数组$res['yes']中, * 而剩下的未中奖的信息保存在$res['no']中, * 最后输出json个数数据给前端页面。 */ foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $rid = get_rand($arr); //根据概率获取奖项id $res['yes'] = $prize_arr[$rid-1]['prize']; //中奖项 unset($prize_arr[$rid-1]); //将中奖项从数组中剔除,剩下未中奖项 shuffle($prize_arr); //打乱数组顺序 for($i=0;$i<count($prize_arr);$i++){ $pr[] = $prize_arr[$i]['prize']; } $res['no'] = $pr; print_r($res['yes']);

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function