Detailed explanation of PHP random red envelope algorithm

墨辰丷
Release: 2023-03-26 16:42:02
Original
1713 people have browsed it

This article mainly introduces the random red envelope algorithm implemented in PHP. It analyzes the principles, implementation methods and related precautions of the PHP random red envelope algorithm in the form of examples. Friends in need can refer to the following

. The details are as follows:

1. Overall design

There are many kinds of algorithms, you can choose by yourself. The main "architecture" is like this, use redis decr( ) command to limit the current, use mysql to record various required data

2. Red envelope algorithm

For the sake of simplicity, the amount of the red envelope is an integer Expressed, assuming that each red envelope contains *2, It is necessary to pre-allocate one bean to each person at the beginning

function randBean($total_bean, $total_packet)
{
  $min_bean = 1;
  $max_bean = 5000;
  $range = 2;
  $total_bean = $total_bean - $total_packet * $min_bean; //每个人预留一个最小值
  $list = [];
  $min = 1;
  while(count($list) < $total_packet){
    $max = floor($total_bean / $total_packet) * $range;
    $bean = rand($min, $max);
    if ($bean <= $max_bean - 1) {
      $list[] = $bean;
      $total_bean -= $bean;
    }
  }
  $list[] = $total_bean;//剩余的金豆作为最后一个红包
  //合并
  foreach ($list as $k => $v) {
    $list[$k] += $min_bean;
  }
  return $list;
}
Copy after login


Multiple statistics


The second one, to simply expand it, the maximum amount of each red envelope is: (remaining amount/number of remaining red envelopes)*3; but it is required to send at least 2 each time Red envelope

The third type, the line segment method, randomly generates a few numbers, divides a straight line into several segments, and the length of each segment (this fluctuates greatly, not very average, if it is restricted The size of each red envelope will be more troublesome)

function abc ($total_bean, $total_packet)
{
  $min = 1;
  $max = $total_bean -1;
  $list = [];
  $maxLength = $total_packet - 1;
  while(count($list) < $maxLength) {
    $rand = mt_rand($min, $max);
    empty($list[$rand]) && ($list[$rand] = $rand);
  }
  $list[0] = 0; //第一个
  $list[$total_bean] = $total_bean; //最后一个
  sort($list); //不再保留索引
  $beans = [];
  for ($j=1; $j<=$total_packet; $j++) {
    $beans[] = $list[$j] - $list[$j-1];
  }
  // return $beans;
  echo &#39;<pre class="brush:php;toolbar:false">&#39;; print_r($beans); echo array_sum($beans);
}
abc(100000, 3);
Copy after login


##The fourth type

:

Because each person has at least 1 bean, each time the number of beans is randomly generated, the remaining number of people * 1 bean must be reserved;

After removing the necessary number of beans, use the remaining number of beans The average value is the maximum value to randomly generate the number of beans this time

Related recommendations:

Random red envelope implemented by php

Algorithm

#phpHow to implement fixed red envelopes and random red envelopes
Algorithm detailed explanation (picture)

php generationRandom red envelope
Algorithm


The above is the detailed content of Detailed explanation of PHP random red envelope algorithm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template