Python实现一个双色球生成程序

最近学习Python,就顺手写一个Demo吧


这里主要是Python的类的概念,还有就是随机函数random.sample()的使用


#coding=utf-8
# filename: redball.py
import random
import math
'这是一个彩票生成程序, 可以随机生成双色球和大乐透'

class newBall:
    _redQuantity = 0
    _blueQuantity = 0
    _redMax = 0
    _blueMax = 0
    _oneRecord = ()
    
    #类的初始化
    def __init__(self, redMax, redQuantity, blueMax, blueQuantity):
        self._redMax = redMax
        self._redQuantity = redQuantity
        self._blueQuantity = blueQuantity
        self._blueMax = blueMax
    #生成一组新的数据
    def produce_new(self):
        #从一个队列里面随机选取self._redQuantity数目的数字
        redBallList = range(1, self._redMax + 1)
        redNewList = random.sample(redBallList, self._redQuantity)
        redNewList = sorted(redNewList)
        #随机生成蓝球
        blueBallList = range(1, self._blueMax + 1)
        blueNewList = random.sample(blueBallList, self._blueQuantity)
        self._oneRecord = redNewList + blueNewList
    
    #在console里面显示结果
    def showResult(self):
        print self._oneRecord

#双色球规则:33个红球中抽取6个,16个蓝球中抽取1个
redball = newBall(33, 6, 16, 1)
redball.produce_new()
redball.showResult()
#大乐透规则:35个红球中抽取5个,12个蓝球中抽取2个
sevenBall = newBall(35, 5, 12, 2)
sevenBall.produce_new()
sevenBall.showResult()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值