generating a random red python

Suppose the amount of red money, Quantity num, and the amount of red money> = num * 0.01

Principle is as follows, from the set of numbers 1 ~ money * 100, the random number num-1, then these numbers are sorted, and inserting money * 100 0 respectively before and after ordered set to form the new set of

With a new set, (after a number - a number before) / 100 obtained in the size of red

Then use red when a red envelope from num take a random collection of both random red envelopes

def redbags(money, num=10):
    import random
    choice = random.sample(range(1, money * 100), num - 1)
    choice.extend([0,money*100])
    choice.sort()
    return [(choice[i + 1] - choice[i]) / 100 for i in range(num)]

 

Guess you like

Origin www.cnblogs.com/lcawen/p/11445904.html