Rob Red Envelopes So happy, then you know the red envelope random algorithm is how?
I have written a fixed random red envelope generation algorithm, as follows.
Input:
- Total of red envelopes
- Number of copies, num
- Control parameters (Control of red envelopes The average difference, the default is 2)
Constraints:
- A minimum of 1 cents per copy, i.e. 0.01
- The number of copies must be a positive integer
- Red Envelope Total <= copies of x0.01
Output
- Random red envelope sequence, sequence length equals bonus number
- King of luck, the largest amount of red envelopes
#-*-coding:cp936-*-# Idea: First random number of M, and then evenly divided into m numbers only and the number of copies, and then the average distribution of money to M personal #ImportRandom def checkparam(total,num): if(Type (num)! = Type (1)):Print "num must be Integer";return False;if(Num <0):Print "num must be Positive Integer";return False;return True; def roll(total,num,cond): Print "= = = Red Envelope algorithm research program = = =" Print "Total", Total,"Yuan money." Points "Num"part." The condition parameter is: ", cond Total *= - if(Checkparam (Total,num)): p = [] average = Total/num# # print "Pre-average", averagePre = [] Allpre =0.0 forCountinchRange0, num): TP = Random.randint (1,Ten**cond) pre.append (TP) Allpre + = TP# # print "Ready for random Sequence", Pre,len (PRE)# # print "Total number of preparations", AllpreOnepre = Round (Total/allpre,cond)## # # print "Reserve single share", Onepre# # print "Total amount of preparation", Onepre * allpre Print '-------'ALLTP =0 forMinchRange0, Len (PRE)-1): TP = Int (onepre*pre[m])if(0==TP): TP =1ALLTP + = TP P.append (tp/100.0) Last = TOTAL-ALLTP P.append (last/100.0) ALLTP + = Last#p. Sort () #sorted (P) [0] Print "The King of luck:", sorted (p) [num-1]#,p[num-1] #random. Shuffle (p) Print "Red envelope sequence", P,len (P)Print "issued in total", alltp/100.0 #random. Shuffle (p) #打乱序列#总金额, number of copies, regulation parameters (regulating average difference)RollTen, -,2)
The following is the actual effect of the operation:
Algorithm ideas:
- Generates NUM random numbers, which are stored in a random list. Num is the number of red envelopes, the range of random numbers is determined by the regulatory parameters, the control parameter is 2, which indicates that the random number range is 1-10**2 (10 of 2).
- Add n random numbers to get a sum of pretotal.
- The average value is obtained by dividing the sum of the red envelopes by random numbers. Average = Total/pretoal.
- The first num-1 random numbers in the random list are multiplied by the average number, which is the amount of the random red envelope, and the last remaining amount is the amount of the first num red envelope.
- To deal with the problem of too small data, in order to ensure that the minimum amount of 0.01 yuan, in the multiplication of the link may be due to the number of floating point rounding, the result is 0, so long this data is set to the minimum amount.
-python implementation of random red envelope generation algorithm