zoukankan      html  css  js  c++  java
  • random.randint()与np.random.randint()的区别

    一.比较两个函数

    先来看看random.randint()

    import random
    for n in range(5):
        for i in range(10):
            print(random.randint(1,5),end=' ')
        print()
    #运行结果
    1 5 5 3 3 1 3 1 5 2 
    4 4 4 4 4 4 3 1 5 2 
    3 2 3 1 1 5 5 1 4 3 
    3 4 4 2 5 5 3 4 4 4 
    3 5 4 5 4 5 4 5 2 4 
    Process finished with exit code 0

    再来看看numpy.random.randint()方法:

    import numpy as np
    for n in range(5):
        for i in range(10):
            print(np.random.randint(1, 5), end=' ')
        print()
    
    #运行结果
    2 4 1 1 1 1 2 2 2 4 
    3 4 3 2 3 4 3 2 2 4 
    2 2 1 2 1 1 3 3 3 4 
    4 1 4 2 4 1 3 4 3 2 
    2 3 3 2 3 4 4 3 4 4 
    Process finished with exit code 0
    看出有什么不同了吗?random.randint()方法里面的取值区间是前闭后闭区间,而np.random.randint()方法的取值区间是前闭后开区间,是它们的最大区别。使用的时候一定要注意。
    再来详细看看这两个方法吧。
    random.randint(a,b[,c])
    #用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b。c是步幅。
    例如:
    1)print(random.randint(12, 20)) #生成的随机数n: 12 <= n <= 20
    2)print(random.randint(20, 20)) #结果永远是20
    3)print(random.randint(20, 10)) #该语句是错误的。因为下限必须小于上限。
    randint.randrange()函数在随机是不包括上限数的。摘自
    numpy.random.randint(low, high=None, size=None, dtype='l')
    #这个方法产生离散均匀分布的整数,这些整数大于等于low,小于high。
    low : int        #产生随机数的最小值
    high : int, optional    #给随机数设置个上限,即产生的随机数必须小于high
    size : int or tuple of ints, optional    #整数,生成随机元素的个数或者元组,数组的行和列
    dtype : dtype, optional    #期望结果的类型

     转自:https://www.jianshu.com/p/f51900e3bac7

  • 相关阅读:
    大约 C++ 几个方面分析--overload, override, overwrite, rewrite
    Catalan数总结
    JAVA该队列中的数组,圆阵队列,链队列
    hdu4770:Lights Against Dudely(回溯 + 修剪)
    Birt
    Activity
    简单实现的Servlet文件上传,并显示
    阿里云centos 6.5 32位安装可视化界面的方法
    cannot mount database in EXCLUSIVE mode
    ORA-00845: MEMORY_TARGET not supported on this system
  • 原文地址:https://www.cnblogs.com/sggggr/p/12196998.html
Copyright © 2011-2022 走看看