详解python实现红包随机生成算法的实例代码

实例如下:

#! /usr/bin/python
# -*- coding: utf-8 -*-
import random
class cdispatch:
def __init__(self,sum,count):
self.sum = sum
self.count=count
#print ‘init here sum =’,sum,’,count =’,count
def __del__(self):
pass
#print ‘run del the class’
def getlistinfo(self):
listinfo=[]
summoney = self.sum*100
for num in range(0,self.count):
if(num == self.count -1):
listinfo.append(float(‘%0.2f’%summoney)/100)
break
bigrand=summoney+1+num-self.count
#print ‘summoney=’,summoney,’num=’,num,’self.count=’,self.count,’big=’,bigrand
try:
a = random.randint(1,int(bigrand))
except:
for i in range(0,num):
print ‘listinfo[%d]’%i,’=’,listinfo[i]
if num >0:
print ‘summoney=’,summoney,’num=’,num,’listinfo[num-1]=’,listinfo[num-1],’self.count=’,self.count,’big=’,bigrand
#print ‘a=’,a
break
summoney -=a
listinfo.append(float(a)/100)
return listinfo
for i in range(0,100000):
dispatch = cdispatch(1.05,5)
listget = dispatch.getlistinfo()
print listget
del dispatch

以上就是详解python实现红包随机生成算法的实例代码的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论