<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>运达&#039;s  blog &#187; 红包</title>
	<atom:link href="https://www.yunda51.com/?feed=rss2&#038;tag=%E7%BA%A2%E5%8C%85" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>PHP+Ajax手机发红包</title>
		<link>https://www.yunda51.com/?p=1673</link>
		<comments>https://www.yunda51.com/?p=1673#comments</comments>
		<pubDate>Tue, 26 Jan 2016 03:50:54 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[php技术]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[红包]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1673</guid>
		<description><![CDATA[PHP发红包基本流程：当输入完红包数量和总金额后，PHP会根据这两个值进行随机分配每个金额，保证每个人都能领取<a href="https://www.yunda51.com/?p=1673" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>PHP发红包基本流程：当输入完红包数量和总金额后，PHP会根据这两个值进行随机分配每个金额，保证每个人都能领取到一个红包，且每个红包金额不等。也就是每个人领取的红包金额要不同，并且所有红包金额总额等于总金额。<strong>如图：</strong></p>
<p><a href="http://www.yunda51.com/wp-content/uploads/2016/01/big.jpg.png"><img class="size-full wp-image-1677 aligncenter" src="http://www.yunda51.com/wp-content/uploads/2016/01/big.jpg.png" alt="big.jpg" width="256" height="210" /></a></p>
<p>php发红包实现原理：</p>
<p>设定总金额为10元，有N个人随机领取：</p>
<p>N=1 第一个</p>
<p>则红包金额=X元；</p>
<p>N=2 第二个</p>
<p>为保证第二个红包可以正常发出，第一个红包金额=0.01至9.99之间的某个随机数</p>
<p>第二个红包=10-第一个红包金额；</p>
<p>N=3 第三个</p>
<p>红包1=0.01至9.99之间的某个随机数</p>
<p>红包2=0.01至(10-红包1-0.01)的某个随机数</p>
<p>红包3=10-红包1-红包2</p>
<p>……</p>
<p>于是我们得到一个规律，在分配当前红包金额时，先预留剩余红白所需最少金额，然后在0.01至总金额-预留金额间取随机数，得到的随机数就是当前红包分配的金额。</p>
<p>实际应用中，程序先将红包金额分配好，即发红包时，红包个数以及每个红包的金额都分配好了，那么用户来抢红包时，我们随机给用户返回一个红包即可。<br />
<strong>jQuery代码：</strong></p>
<pre class="wp-code-highlight prettyprint">
$(function() { 
    $(&quot;button&quot;).click(function() { 
        $.ajax({ 
            type: &#039;POST&#039;, 
            url: &#039;bao.php&#039;, 
            dataType: &#039;json&#039;, 
            beforeSend: function() { 
                $(&quot;#result&quot;).html(&#039;正在分配红包&#039;); 
            }, 
            success: function(json) { 
                if (json.msg == 1) { 
                    var str = &#039;&#039;; 
                    var res = json.res; 
                    $.each(res, 
                    function(index, array) { 
                        str += &#039;&lt;p&gt;第&lt;span&gt;&#039; + array[&#039;i&#039;] + &#039;&lt;/span&gt;个红包，
                        金额&lt;span&gt;&#039; + array[&#039;money&#039;] + &#039;&lt;/span&gt;元，余额&lt;span&gt;&#039; + 
                        array[&#039;total&#039;] + &#039;元&lt;/span&gt;&lt;/p&gt;&#039;; 
                    }); 
                    $(&quot;#result&quot;).html(str); 
                } else { 
                    $(&quot;#result&quot;).html(&#039;数据出错！&#039;); 
                } 
            } 
        }); 
    }); 
});
</pre>
<p><strong>PHP代码：bao.php</strong></p>
<pre class="wp-code-highlight prettyprint">$total=20;//红包总金额    
$num=10;// 分成10个红包，支持10人随机领取    
$min=0.01;//每个人最少能收到0.01元    
  
for ($i=1;$i&amp;lt;$num;$i++)    
{    
    $safe_total=($total-($num-$i)*$min)/($num-$i);//随机安全上限    
    $money=mt_rand($min*100,$safe_total*100)/100;    
    $total=$total-$money;   
      
    echo &#039;第&#039;.$i.&#039;个红包：&#039;.$money.&#039; 元，余额：&#039;.$total.&#039; 元 &#039;;    
}    
echo &#039;第&#039;.$num.&#039;个红包：&#039;.$total.&#039; 元，余额：0 元&#039;;
</pre>
<p><strong>点击下载：</strong><a style="background: transparent none repeat scroll 0% 0%;" href="http://pan.baidu.com/s/1eQUrneq" target=""><img class="alignnone size-full wp-image-1075" title="btn_load" src="http://www.yunda51.com/wp-content/uploads/2013/11/btn_load3.png" alt="" width="86" height="28" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1673</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
