random模块

random模块

  • random.random() 生成一个0到1的随机浮点数

    >>> random.random()
    0.9505298542730263
    
    >>> random.random()
    0.04196790936158523
    
  • random.uniform(a, b) 生成一个a到b的随机浮点数

    >>> random.uniform(1,10)
    2.4167535667959816
    >>> random.uniform(1,10)
    9.17209138619443
    
  • random.randint(a, b) 生成一个a到b的随机整数

    >>> random.randint(1, 100)
    14
    >>> random.randint(1, 100)
    5
    >>> random.randint(1, 100)
    54
    
  • random.randrange(start, stop=None, step=1) 生成一个从start到stop间隔为step的整数

    >>> random.randrange(1 ,10 ,2)
    9
    >>> random.randrange(1 ,10 ,2)
    3
    >>> random.randrange(1 ,10 ,2)
    1
    
  • random.choice(seq) 从非空序列seq中选择一个随机元素

    >>> s = "pneumonoultramicroscopicsilicovolcanoconiosis"
    >>> random.choice(s)
    'c'
    >>> random.choice(s)
    'o'
    >>> random.choice(s)
    'u'
    >>> random.choice(s)
    'i'
    >>> random.choice(s)
    's'
    

posted on 2021-07-07 15:32  MidSummer丶  阅读(79)  评论(0)    收藏  举报

导航