python 实现红包随机生成算法的简单实例

yipeiwu_com6年前Python基础

实例如下:

</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

以上这篇python 实现红包随机生成算法的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

学习python (1)

先推荐一个学习python的好网址简明 Python 教程 Swaroop, C. H. 著 沈洁元  译在线教程的网址:http://www.woodpecker.org.cn:...

解决Django 在ForeignKey中出现 non-nullable field错误的问题

在django的model中建立了如下的类 class UserType(models.Model): name = models.CharField(max_length=40,...

深入理解Python3中的http.client模块

http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块。 http.client 是一个底层的 HTTP 协议客户端,被更高层的 urlli...

Python socket实现的文件下载器功能示例

本文实例讲述了Python socket实现的文件下载器功能。分享给大家供大家参考,具体如下: 文件下载器 先写客户端再写服务端 1.tcp下载器客户端 import socket...

TensorFlow 合并/连接数组的方法

如下所示: import tensorflow as tf a = tf.Variable([4,5,6]) b = tf.Variable([1,2,3]) c = tf.co...