Home > Backend Development > PHP Tutorial > Implementation of random red envelope function in PHP

Implementation of random red envelope function in PHP

小云云
Release: 2023-03-19 17:00:01
Original
1721 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 it. I hope it can help everyone.

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


Second type, 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 red envelopes each time

The third type, line segment method, randomly generates several numbers, divides a straight line into several segments, and the length of each segment (this fluctuation is relatively large, not very average, if the size of each red envelope is limited, it 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, then the number of beans is randomly generated every time Reserve the remaining number of beans * 1 bean;After removing the necessary remaining bean numbers, use the average of the remaining bean numbers as the maximum to randomly generate the current bean number

Related recommendations:

Detailed explanation of how php implements fixed red envelopes and random red envelope algorithms (picture)

php generates random red envelope algorithm

Generate a specified number of random red envelopes

The above is the detailed content of Implementation of random red envelope function in PHP. 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