zoukankan      html  css  js  c++  java
  • Python基础-random模块及随机生成11位手机号

    import random

    # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围
    # print(random.randint(1, 20)) # 随机整数,顾头顾尾
    # print(random.choice('sdfsd233')) # 随机取一个元素
    # print(random.sample('hello234234史蒂夫34', 4))#从序列中随机取几个元素,返回是一个list
    # f =random.uniform(1, 9)  # 随机取浮点数,可以指定范围
    # x = [1, 2, 3, 4, 6, 7]
    # random.shuffle(x) # 洗牌,打乱顺序,会改变原list的值
    # print(x)

    利用随机数生成11位手机号
    import string
    def phone_num(num):
        all_phone_nums=set()
        num_start = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '158', '159', '157', '182', '187', '188',
               '147', '130', '131', '132', '155', '156', '185', '186', '133', '153', '180', '189']
        for i in range(num):
            start = random.choice(num_start)
            end = ''.join(random.sample(string.digits,8))
            res = start+end+'
    '
            all_phone_nums.add(res)
        with open('phone_num.txt','w',encoding='utf-8') as fw:
            fw.writelines(all_phone_nums)
    phone_num(1000)
  • 相关阅读:
    gems gems gems
    poj 6206 Apple
    lightoj1341唯一分解定理
    lightoj1370欧拉函数
    约瑟夫环lightoj1179
    拓展欧几里得算法
    RMQ算法
    poj1502MPI Maelstrom
    poj1860Currency Exchange
    生成全排列
  • 原文地址:https://www.cnblogs.com/niuniu2018/p/7806493.html
Copyright © 2011-2022 走看看