python 实现红包随机生成算法的简单实例
作者:jingxian 时间:2022-07-07 17:32:48
实例如下:
</pre><pre name="code" class="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
标签:红包,随机,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在SQL server2005数据库下创建计划任务
2008-12-26 09:19:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081231122259529.jpg)
详解python连接telnet和ssh的两种方式
2023-02-10 01:27:14
python logging设置level失败的解决方法
2022-03-23 13:54:40
浅析python 中__name__ = '__main__' 的作用
2022-07-21 16:32:28
两个css郁闷的发现
2007-12-16 15:31:00
DOM_window对象属性之--clipboardData对象操作代码
2011-02-05 10:49:00
提升设计品质的8个布局方案[译]
2010-03-18 16:06:00
![](https://img.aspxhome.com/file/UploadPic/20103/18/01-33s.jpg)
Python实现简单2048小游戏
2023-08-13 00:19:18
![](https://img.aspxhome.com/file/2023/3/71553_0s.jpg)
Python基于Tkinter模块实现的弹球小游戏
2022-11-25 15:32:16
![](https://img.aspxhome.com/file/2023/4/94114_0s.png)
asp如何制作一个弹出式的调查窗口?
2010-06-29 21:24:00
Python 开发工具通过 agent 代理使用的方法
2022-10-19 05:15:31
CSS3 + HTML5 实现未来 Web 设计
2010-01-25 12:17:00
![](https://img.aspxhome.com/file/UploadPic/20101/25/232021rjs-74s.png)
PHP三层结构(上) 简单三层结构
2023-06-20 06:36:07
![](https://img.aspxhome.com/file/2023/6/553461_0s.png)
Web开发技术发展史话
2011-04-25 19:16:00
如何解决国外空间显示乱码问题
2007-11-18 14:28:00
python数据写入Excel文件中的实现步骤
2023-11-19 23:11:45
Python3 使用pip安装git并获取Yahoo金融数据的操作
2023-11-17 21:56:48
![](https://img.aspxhome.com/file/2023/1/63941_0s.jpg)
基于Tensorflow搭建一个神经网络的实现
2024-01-02 06:09:35
![](https://img.aspxhome.com/file/2023/0/77620_0s.png)
降低锁竞争 减少MySQL用户等待时间
2011-03-11 18:58:00
快速解决vue.js 模板和jinja 模板冲突的问题
2023-04-04 12:49:59
![](https://img.aspxhome.com/file/2023/5/81785_0s.jpg)