zoukankan      html  css  js  c++  java
  • Python的Random模块

    random模块

    random模块提供了一些生成随机数的函数,一些介绍为了简单省略random.

    • random():返回在范围大于等于0.0,且小于1.0内的随机浮点数
    • randrange(stop):返回大于等于0,且小于
    • randrange(start, stop[, step]):还是randrange函数,返回在范围大于等于start,且小于stop内,步长为step的随机整数
    • randint(a, b):返回在范围大于等于a,且小于等于b的随机整数
    # coding=utf-8
    #!/usr/bin/python3
    
    import random
    
    # 0.0 <= x < 1.0随机数
    print('0.0 <= x < 1.0随机数')
    for i in range(0, 10):
        x = random.random()
        print(x)
        pass
    
    # 0 <= x < 5随机数
    print('0 <= x < 5随机数')
    for i in range(0, 10):
        x = random.randrange(5)
        print(x)
        pass
    
    # 5 <= x < 10随机数
    print('5 <= x < 10随机数')
    for i in range(0, 10):
        x = random.randrange(5, 10)
        print(x)
        pass
    
    # 5 <= x <= 10随机数
    print('5 <= x <= 10随机数')
    for i in range(0, 10):
        x = random.randint(5, 10)
        print(x)
    

    运行结果:

    0.0 <= x < 1.0随机数
    0.34029878291607385
    0.2282371197599904
    0.6646715464011697
    0.028942492371459738
    0.8504465561709386
    0.5010168923934861
    0.7747864321671818
    0.1241085608890693
    0.31135409609401055
    0.2287655076334516
    0 <= x < 5随机数
    3 3 1 2 1 0 2 2 2 4
    5 <= x < 10随机数
    5 7 7 5 8 6 7 6 8 7

  • 相关阅读:
    抽卡 状压DP+期望DP+系数递推
    20190903考试反思
    20190823考试反思
    约瑟夫类问题研究
    树位DP
    20190823考试反思
    20190820考试反思
    20190818考试反思
    20190817考试反思
    PowerBuilder--Aes128加解密
  • 原文地址:https://www.cnblogs.com/coding365/p/12701244.html
Copyright © 2011-2022 走看看