Java implementation of imitation wechat red envelope allocation rules

Recently, sending red envelopes to celebrate the new year has become a new trend. As a program ape, he is much more curious about algorithms than about red envelopes. Here is a random red envelope allocation strategy he thought of. Please give us more advice.

Algorithm Introduction

1、 Limit of red envelope amount

For wechat red envelopes, we know that no one is random. The minimum red envelope is 1 point and the maximum amount is 200 yuan. Here we also set the range of red envelopes. The following code unifies the unit of money as points.

2、 Judge whether the amount of the red envelope is legal. Note that this step is accompanied by the whole algorithm. We should not only judge whether the amount is legal before allocating the red envelope, but also judge whether the remaining amount is legal after everyone provisional random amount.

3、 Randomly generate a red envelope. Here, we randomly generate a red envelope between minmoney and maxmoney. After generating a red envelope, we need to judge whether the remaining money is a legal red envelope. If it is not a legal red envelope, we will regenerate the distribution scheme. When regenerating the distribution scheme, we need to determine one thing, Is the generated red envelope too large or too small? If the red envelope is too large, a small value will be randomly added to a red envelope of the current red envelope amount next time. If the red envelope amount is too small, we will generate a red envelope from a red envelope amount to a large value.

4、 Realize red envelope allocation. Here, in order to avoid a red envelope occupying a lot of money, we need to set the maximum amount of the non last red envelope, which we set to N times the average value of the red envelope; With the methods in 1, 2 and 3, we can realize the distribution of red envelopes.

Evaluation of red envelope distribution scheme

The basic algorithm of red envelope is introduced above. Let's verify the algorithm. Suppose there is a red envelope of 200 yuan and 100 copies, let's take a look at the final allocation scheme.

Complete code

For more highlights, please click Android wechat development tutorial summary and Java wechat development tutorial summary. You are welcome to learn and read.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>