Group red packet algorithm and group red packet algorithm
Today is the last day of the Spring Festival. There is nothing to do. It is just that the students in the group will discuss the algorithm of group red packets.
A close-up algorithm is provided. The general idea is to find the red envelope that has been sent and find its random vibration slope.
Let's take a look at the algorithm:
# Coding = gbkimport randomimport sys # print random. randint (0, 99) # print "=", random. uniform (0, 0.99) def calRandomValue (min, max, total, num): print min, max, total, numtotal = float (total) num = int (num) min = 0.01if (num <1): returnif num = 1: print "% d: %. 2f "% (num, total) returni = 1total_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-imax = max/kmonney = random. randint (int (min * 100), int (max * 100) monney = float (monney)/100total_money = total_money-monneyprint "% d the number of red packets received by individuals is %. 2f, balance: %. 2f "% (I, monney, total_money) I + = 1 print" % d: %. 2f, balance: %. 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)
Run:
Python 2.py 0.01 10 20 30
0.01 10 20 30
1st people receive 0.42 red packets, with a balance of 19.58
2nd people receive 0.12 red packets, with a balance of 19.46
3rd people receive 0.24 red packets, with a balance of 19.22
4th people receive 1.10 red packets, with a balance of 18.12
5th people receive 1.45 red packets, with a balance of 16.67
6th people receive 0.21 red packets, with a balance of 16.46
7th people receive 0.34 red packets, with a balance of 16.12
8th people receive 0.16 red packets, with a balance of 15.96
9th people receive 1.38 red packets, with a balance of 14.58
10th people receive 0.86 red packets, with a balance of 13.72
11th people receive 1.18 red packets, with a balance of 12.54
12th people receive 0.93 red packets, with a balance of 11.61
13th people receive 0.70 red packets, with a balance of 10.91
14th people receive 0.60 red packets, with a balance of 10.31
15th people receive 0.03 red packets, with a balance of 10.28
16th people receive 0.61 red packets, with a balance of 9.67
17th people receive 1.37 red packets, with a balance of 8.30
18th people receive 0.67 red packets, with a balance of 7.63
19th people receive 0.28 red packets, with a balance of 7.35
20th people receive 0.96 red packets, with a balance of 6.39
21st people receive 1.30 red packets, with a balance of 5.09
22nd people receive 1.08 red packets, with a balance of 4.01
23rd people receive 1.12 red packets, with a balance of 2.89
24th people receive 0.75 red packets, with a balance of 2.14
25th people receive 0.64 red packets, with a balance of 1.50
26th people receive 0.37 red packets, with a balance of 1.13
27th people receive 0.01 red packets, with a balance of 1.12
28th people receive 0.26 red packets, with a balance of 0.86
29th people receive 0.31 red packets, with a balance of 0.55
30th people receive 0.55 red packets, with a balance of 0.00
Python 2.py 0.01 10 20 2
0.01 10 20 2
1st people receive 1.62 red packets, with a balance of 18.38
2nd people receive 18.38 red packets, with a balance of 0.00
Python 2.py 0.01 10 20 3
0.01 10 20 3
1st people receive 4.58 red packets, with a balance of 15.42
2nd people receive 11.22 red packets, with a balance of 4.20
3rd people receive 4.20 red packets, with a balance of 0.00