lowest=0.01 yuan, Minimum amount
The operation is an integer, the smallest renminbi unit is a cent, so there are 2 decimal places, at least 0.01 yuan
Send n people, take 1 to 100 random number n
n the sum of the number of the total amount, with the total red sum to remove, get each part of the amount per=total/n
Start to send red envelopes if the number of N unlucky to take 0, can not give 0, give a minimum number lowest 0.01 yuan
Base it is permount=pernum*per/100
Send to the penultimate, the last one can not be issued according to the formula, the total number of total-has been issued, (reason is the amount of each
When two decimal places are reserved, rounding is used)
To illustrate:
6 Yuan to 3 people
From 0 to 100 take 3 random numbers, such as 20 50 60
Each amount 6/(20+50+60) =0.0461538461538462 at this time to calculate faster, reserved two decimal places 0.05 yuan
Loss of precision, the last man algorithm to pay attention to
The first person to get 0.05*20=1 yuan
The second person gets 0.05*50=2.5 yuan
A third person gets 6-(1+2.5) = 2.5 yuan
Attach Python code:
#-*-Coding:utf-8-*-
# 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 #
Import Random
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 "All", total, "Yuan Qian." Cent ", NUM," copy. The condition parameter is: ", cond
Total *= 100
if (Checkparam (total,num)):
p = []
Average = Total/num
# # print "Pre-average", average
Pre = []
Allpre = 0.0
For count in range (0,num):
TP = Random.randint (1,10**cond)
Pre.append (TP)
Allpre + = TP
# # print "Ready for random Sequence", Pre,len (PRE)
# # print "Total number of preparations", allpre
Onepre = Round (Total/allpre,cond)
##
# # print "Reserve single share", Onepre
# # print "Total amount of preparation", Onepre * allpre
print '-------'
ALLTP = 0
For M in range (0,len (pre)-1):
TP = Int (onepre*pre[m])
if (0==TP):
TP = 1
ALLTP + = TP
P.append (tp/100.0)
Last = TOTAL-ALLTP
P.append (last/100.0)
ALLTP + = Last
#p. Sort ()
#sorted (P) [0]
Print "Luck King:", Sorted (p) [Num-1]#,p[num-1]
#random. Shuffle (P)
print "Red envelope sequence", P,len (p)
Print "issued in total", alltp/100.0
[Email protected]:~/tmp# VI sendmoney.py
#-*-Coding:utf-8-*-
# 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 #
Import Random
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 "All", total, "Yuan Qian." Cent ", NUM," copy. The condition parameter is: ", cond
Total *= 100
if (Checkparam (total,num)):
p = []
Average = Total/num
# # print "Pre-average", average
Pre = []
Allpre = 0.0
For count in range (0,num):
TP = Random.randint (1,10**cond)
Pre.append (TP)
Allpre + = TP
# # print "Ready for random Sequence", Pre,len (PRE)
# # print "Total number of preparations", allpre
Onepre = Round (Total/allpre,cond)
##
# # print "Reserve single share", Onepre
# # print "Total amount of preparation", Onepre * allpre
print '-------'
ALLTP = 0
For M in range (0,len (pre)-1):
TP = Int (onepre*pre[m])
if (0==TP):
TP = 1
ALLTP + = TP
P.append (tp/100.0)
Last = TOTAL-ALLTP
P.append (last/100.0)
ALLTP + = Last
#p. Sort ()
#sorted (P) [0]
Print "Luck King:", 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)
Roll (3,5,2)
Algorithm of Lucky Red envelopes