当前位置:Gxlcms > Python > 详解python实现红包随机生成算法的实例代码

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

时间:2021-07-01 10:21:17 帮助过:112人阅读

实例如下:

  1. </pre><pre name="code" class="python">#! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import random
  4. class CDispatch:
  5. def __init__(self,sum,count):
  6. self.sum = sum
  7. self.count=count
  8. #print 'init here sum =',sum,',count =',count
  9. def __del__(self):
  10. pass
  11. #print 'run del the class'
  12. def getListInfo(self):
  13. listInfo=[]
  14. sumMoney = self.sum*100
  15. for num in range(0,self.count):
  16. if(num == self.count -1):
  17. listInfo.append(float('%0.2f'%sumMoney)/100)
  18. break
  19. bigRand=sumMoney+1+num-self.count
  20. #print 'sumMoney=',sumMoney,'num=',num,'self.count=',self.count,'big=',bigRand
  21. try:
  22. a = random.randint(1,int(bigRand))
  23. except:
  24. for i in range(0,num):
  25. print 'listInfo[%d]'%i,'=',listInfo[i]
  26. if num >0:
  27. print 'sumMoney=',sumMoney,'num=',num,'listInfo[num-1]=',listInfo[num-1],'self.count=',self.count,'big=',bigRand
  28. #print 'a=',a
  29. break
  30. sumMoney -=a
  31. listInfo.append(float(a)/100)
  32. return listInfo
  33. for i in range(0,100000):
  34. dispatch = CDispatch(1.05,5)
  35. listGet = dispatch.getListInfo()
  36. print listGet
  37. del dispatch

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

人气教程排行