Home > Article > WeChat Applet > Share a WeChat group red envelope algorithm example code
Today is the last day of work. There is nothing to do. It just so happens that the students in the group are discussing the algorithm of group red envelopes.
A close-up of an algorithm. The general idea is to find red envelopes that have been distributed and find their random vibration slope.
Let’s take a look at the algorithm:
#coding=gbk import random import sys #print random.randint(0, 99) #print "====", random.uniform(0, 0.99) def calRandomValue(min, max, total, num): print min, max, total, num total = float(total) num = int(num) min = 0.01 if(num < 1): return if num == 1: print "第%d个人拿到红包数为:%.2f" %(num, total) return i = 1 total_money = total #rtotal = (total*100 - min*num*100)/100 while( i < num ): max = total_money - min*(num- i) k = int((num-i)/2) if num -i <= 2: k = num -i max = max/k monney = random.randint(int(min*100), int(max*100)) monney = float(monney)/100 total_money = total_money - monney print "第%d个人拿到红包数为:%.2f, 余额为: %.2f" %(i, monney, total_money) i += 1 print "第%d个人拿到红包数为:%.2f, 余额为: %.2f" %(i, total_money, 0.0) if __name__ == "__main__": min = sys.argv[1] max = sys.argv[2] total = sys.argv[3] num = sys.argv[4] calRandomValue(min, max, total, num)
Execution:
python 2.py 0.01 10 20 300.01 10 20 30第1个人拿到红包数为:0.42, 余额为: 19.58第2个人拿到红包数为:0.12, 余额为: 19.46第3个人拿到红包数为:0.24, 余额为: 19.22第4个人拿到红包数为:1.10, 余额为: 18.12第5个人拿到红包数为:1.45, 余额为: 16.67第6个人拿到红包数为:0.21, 余额为: 16.46第7个人拿到红包数为:0.34, 余额为: 16.12第8个人拿到红包数为:0.16, 余额为: 15.96第9个人拿到红包数为:1.38, 余额为: 14.58第10个人拿到红包数为:0.86, 余额为: 13.72第11个人拿到红包数为:1.18, 余额为: 12.54第12个人拿到红包数为:0.93, 余额为: 11.61第13个人拿到红包数为:0.70, 余额为: 10.91第14个人拿到红包数为:0.60, 余额为: 10.31第15个人拿到红包数为:0.03, 余额为: 10.28第16个人拿到红包数为:0.61, 余额为: 9.67第17个人拿到红包数为:1.37, 余额为: 8.30第18个人拿到红包数为:0.67, 余额为: 7.63第19个人拿到红包数为:0.28, 余额为: 7.35第20个人拿到红包数为:0.96, 余额为: 6.39第21个人拿到红包数为:1.30, 余额为: 5.09第22个人拿到红包数为:1.08, 余额为: 4.01第23个人拿到红包数为:1.12, 余额为: 2.89第24个人拿到红包数为:0.75, 余额为: 2.14第25个人拿到红包数为:0.64, 余额为: 1.50第26个人拿到红包数为:0.37, 余额为: 1.13第27个人拿到红包数为:0.01, 余额为: 1.12第28个人拿到红包数为:0.26, 余额为: 0.86第29个人拿到红包数为:0.31, 余额为: 0.55第30个人拿到红包数为:0.55, 余额为: 0.00 python 2.py 0.01 10 20 20.01 10 20 2第1个人拿到红包数为:1.62, 余额为: 18.38第2个人拿到红包数为:18.38, 余额为: 0.00 python 2.py 0.01 10 20 30.01 10 20 3第1个人拿到红包数为:4.58, 余额为: 15.42第2个人拿到红包数为:11.22, 余额为: 4.20第3个人拿到红包数为:4.20, 余额为: 0.00
[Related recommendations]
1. Share an Android to implement WeChat automatic Code examples for grabbing red envelopes
2. Sharing example tutorials on developing WeChat public accounts for credit card payment
3. WeChat payment for WeChat development
4. Red Packet Interface Development Example Code for Mini Program
5. Share the WeChat public account development cash red envelope example tutorial
The above is the detailed content of Share a WeChat group red envelope algorithm example code. For more information, please follow other related articles on the PHP Chinese website!