Thinking and summarizing of red envelope algorithm

Source: Internet
Author: User

Reference:

http://mp.weixin.qq.com/s?__biz=MzI2NjA3NTc4Ng==&mid=402360599&idx=1&sn= 69318b235e0e8b402d6fc566a3355af4&scene=0#wechat_redirect

Go to the "How is the random algorithm of red envelopes" to see more people's algorithms.

Recently seen to rob red envelopes so fire, long ago want to try to rob red envelopes function try to do. Just see the reference URL today, it produced a summary of the article.

My early logical guess?

    1. When sending red envelopes, the amount of red envelopes and the number of red envelopes are stored in memory. Then, when the user clicks on the "split red envelope", the server interface request, the server logic to the user calculated a random value (how much money? ), which is returned to the front end. In turn.
    2. Each time a random value is calculated, the remaining amount of money is used in addition to the remainder of the person to remove the red envelopes, to get an average, using this average to balance each of the users of the red envelopes to obtain the funds.
    3. And there is no third, thank you.

However, after reading the reference article, the idea still has certain improvement. In each user click "Split red Envelopes" when, and not as I imagined to calculate how much money users get red envelopes, but in the time of sending red envelopes (that is, the amount of input and the number of red envelopes, after payment and submitted to the server), the server has calculated the amount of each red envelope and put into memory. Then when each user clicks the "Split red envelope", the server directly to the "result" (the value of the red envelope) to the front end to complete the operation of a red envelope. From the performance point of view, this method is obviously better than my previous ideas, but, from the storage space analysis, if the number of red envelopes sent too much, it will be very memory consumption.

===============================================================================

Logic code:

<?php

$total _money = 1000;//The total amount of red envelopes, units: minutes
$num = number of red envelopes issued by 8;//
$min _money = 1;//How much does everyone get at least?

for ($i = 1; $i <= $num; $i + +) {
if ($i = = $num) {//The last red envelope is directly equal to the balance
$money = $total _money;
$total _money = 0;
} else {
$max _money is an important part of the whole algorithm, if the value of the red envelope is determined by the fairness of the activity.
This is only a relatively simple way, if you really want to be a function to do, here the Max_money value of the algorithm must be optimized.
$max _money = $total _money/($num-$i);//$total _money equivalent to the balance of the entire red envelope, $num-$i equivalent to the remaining number of people without a red envelope
$money = Mt_rand ($min _money, $max _money);
$total _money-= $money;
}
$money/= 100;
$tmp = $total _money/100;
echo "{$i} red envelopes with a value of {$money}, the balance is: {$tmp} yuan \ n";

===============================================================================

Thinking and summarizing of red envelope algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.