zoukankan      html  css  js  c++  java
  • Python基础系列讲解——random模块随机数的生成

    随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等。Python内置的random模块提供了生成随机数的方法,使用这些方法时需要导入random模块。

    import random
    

    下面介绍下Python内置的random模块的几种生成随机数的方法。

    1、random.random() 随机生成 0 到 1 之间的浮点数[0.0, 1.0) 。

    print("random: ", random.random())
    #random:  0.5714025946899135
    

    2、random.randint(a , b) 随机生成 a 与 b 之间的整数[a, b]。

    print("randint: ", random.randint(6,8))
    #randint:  8
    

    3、random.randrange(start,stop,step)按步长step随机在上下限范围内取一个随机数。

    print("randrange: ",random.randrange(20,100,5))
    #randrange:  85
    

    4、random.uniform(a, b) 随机生成 a 与 b 之间的浮点数[a, b]。

    print("uniform: ",random.uniform(5,10))
    #uniform:  5.119790163375776
    

    5、random.choice() 从列表中随机取出一个元素,比如列表、元祖、字符串等。注意的是,该方法需要参数非空,否则会抛出 IndexError 的错误。

    print("choice: ",random.choice("www.yuanxiao.net"))
    #choice:  y
    

    6、random.shuffle(items) 把列表 items 中的元素随机打乱。注意的是,如果不想修改原来的列表,可以使用 copy 模块先拷贝一份原来的列表。

    num = [1, 2, 3, 4, 5]
    random.shuffle(num)
    print("shuffle: ",num)
    #shuffle:  [1, 3, 5, 4, 2]
    

    7、random.sample(items, n) 从列表 items 中随机取出 n 个元素。

    num = [1, 2, 3, 4, 5]
    print("sample: ",random.sample(num, 3))
    #sample:  [4, 1, 5]
    

    Python 的random模块产生的随机数其实是伪随机数,依赖于特殊算法和指定不确定因素(种子seed)来实现。如randint方法生成一定范围内的随机数,会先指定一个特定的seed,将seed通过特定的随机数产生算法,得到一定范围内随机分布的随机数。因此对于同一个seed值的输入产生的随机数会相同,省略参数则意味着使用当前系统时间秒数作为种子值,达到每次运行产生的随机数都不一样。

    random.seed(2)
    print("random: ", random.random())
    #random:  0.9560342718892494
    
    random.seed(3)
    print("random: ", random.random())
    #random:  0.23796462709189137
    
    random.seed(3)#同一个种子值,产生的随机数相同
    print("random: ", random.random())
    #random:  0.23796462709189137
    

    numpy库也提供了random模块,用于生成多维度数组形式的随机数。使用时需要导入numpy库。

    import numpy as np
    

    下面介绍下numpy库的random模块的几种生成随机数的方法。
    1、numpy.random.rand(d0,d1,…,dn)

    • rand函数根据给定维度生成[0,1]之间的数据,包含0,不包含1
    • dn表格每个维度
    • 返回值为指定维度的array
    print("np.random.rand:
     {}".format(np.random.rand(4,2))) # shape: 4*3
    """
    np.random.rand:
     [[0.5488135  0.71518937]
     [0.60276338 0.54488318]
     [0.4236548  0.64589411]
     [0.43758721 0.891773  ]]
    """
    叉车配件
    print("np.random.rand:
     {}".format(np.random.rand(4,3,2))) # shape: 4*3*2
    """
    np.random.rand:
     [[[0.96366276 0.38344152]
      [0.79172504 0.52889492]
      [0.56804456 0.92559664]]
    
     [[0.07103606 0.0871293 ]
      [0.0202184  0.83261985]
      [0.77815675 0.87001215]]
    
     [[0.97861834 0.79915856]
      [0.46147936 0.78052918]
      [0.11827443 0.63992102]]
    
     [[0.14335329 0.94466892]
      [0.52184832 0.41466194]
      [0.26455561 0.77423369]]]
    """
    

    2、numpy.random.randn(d0,d1,…,dn)

    • randn函数返回一个或一组样本,具有标准正态分布。
    • dn表格每个维度
    • 返回值为指定维度的array
    • 标准正态分布—-standard normal distribution
    • 标准正态分布又称为u分布,是以0为均值、以1为标准差的正态分布,记为N(0,1)。
    print("np.random.randn:
     {}".format(np.random.randn())) # 当没有参数时,返回单个数据
    """
    np.random.randn:
     2.2697546239876076
    """
    print("np.random.randn:
     {}".format(np.random.randn(2,4)))
    """
    np.random.randn:
     [[-1.45436567  0.04575852 -0.18718385  1.53277921]
     [ 1.46935877  0.15494743  0.37816252 -0.88778575]]
    """
    print("np.random.randn:
     {}".format(np.random.randn(4,3,2)))
    """
    np.random.randn:
     [[[-1.98079647 -0.34791215]
      [ 0.15634897  1.23029068]
      [ 1.20237985 -0.38732682]]
    
     [[-0.30230275 -1.04855297]
      [-1.42001794 -1.70627019]
      [ 1.9507754  -0.50965218]]
    
     [[-0.4380743  -1.25279536]
      [ 0.77749036 -1.61389785]
      [-0.21274028 -0.89546656]]
    
     [[ 0.3869025  -0.51080514]
      [-1.18063218 -0.02818223]
      [ 0.42833187  0.06651722]]]
    """
    

    3、numpy.random.randint(low, high=None, size=None, dtype=’l’)

    • 返回随机整数,范围区间为[low,high),包含low,不包含high
    • 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
    • high没有填写时,默认生成随机数的范围是[0,low]
    print("np.random.randint:
     {}".format(np.random.randint(1,size=5)))# 返回[0,1)之间的整数,所以只有0
    """
    np.random.randint:
     [0 0 0 0 0]
    """
    print("np.random.randint:
     {}".format(np.random.randint(1,5)))# 返回1个[1,5)时间的随机整数
    """
    np.random.randint:
     2
    """
    print("np.random.randint:
     {}".format(np.random.randint(-5,5,size=(2,2))))
    """
    np.random.randint:
     [[-5 -3]
     [ 2 -3]]
    """
    

    4、numpy.random.seed()

    • np.random.seed()的作用:使得随机数据可预测。
    • 当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数

  • 相关阅读:
    四十九、在SAP中查看程序资源结构对象
    四十八、在SAP中函数参数的使用
    四十七、在SAP中,把功能区块整合成一个函数,通过调用函数的办法使代码简洁明了
    四十六、SAP的Message中E和W区别
    四十五、SAP中Message的管理
    四十四、在SAP中冻结第一行表头
    四十三、在SAP中初始化勾选值
    四十二、在SAP中添加单选框
    四十一、在SAP中添加多条件选择框
    四十、SAP中CASE语句用法
  • 原文地址:https://www.cnblogs.com/xyou/p/10530611.html
Copyright © 2011-2022 走看看