zoukankan      html  css  js  c++  java
  • random模块练习

    2、写一个产生双色球号码的程序,输入几就产生多少条
    1、每次产生的里面不能有重复的
    2、产生的号码要 1 2 3 4 5 6 7
    1-32
    1-17
    01 02 03 04 05 06 07
     
    第一种使用list写法 
    import random

    number = input('number:').strip()
    if number.isdigit() and int(number)>0:
    l = [] 这行解决重复
    while True:
    red = random.sample(range(1,34),6)
    red.sort()
    blue = random.sample(range(1,18),1)
    result = red + blue
    result = [ str(ball).zfill(2) for ball in result]
    seq = ' '.join(result)
    if seq not in l:
    l.append(seq)
    print('生成的双色球号码是:红球:%s 蓝球是:%s'%(' '.join(result[:6]),result[-1]))
    with open('seq.txt', 'w', encoding='utf-8') as fw:
    fw.writelines([line+' ' for line in l])#writelines解决写换行问题
    if int(number) == len(l): 这行解决个数一致和循环的结束 否则一直是死循环
    break
    第二种使用集合写法

    import random
     
    reds = [ str(ball).zfill(2) for ball in range(1,34)]
    blues = [ str(ball).zfill(2) for ball in range(1,18)]
     
     
    def seq():
    number = input('number:').strip()
    if number.isdigit() and int(number)>0:
    l = set()
    while int(number)!=len(l):
    red = random.sample(reds,6)
    red.sort()
    blue = random.sample(blues,1)
    result = red + blue
    seq = ' '.join(result)+' '
    l.add(seq)
    print('生成的双色球号码是:红球:%s 蓝球是:%s'%(' '.join(result[:6]),result[-1]))
     
    with open('seq.txt','w',encoding='utf-8') as fw:
    fw.writelines(l)
     
     
    seq()
  • 相关阅读:
    osworkflow
    用Flash做报表,推荐使用Flash饼图
    ANT 发布项目中 build.xml 文件的详细配置
    tomcat 修改java后不重启的方法
    工厂方法(Factory Method)模式
    NSRunLoop概述和原理
    使用NSOperationQueue简化多线程开发
    使用Grad Central Dispatch简化iPhone开发
    进度显示例子学习
    深入浅出 iOS 之多线程
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/14525358.html
Copyright © 2011-2022 走看看