python中np.random.randint()

In [1]:np.random.randint(8,10,size = 20)
Out[1]:
array([8, 9, 9, 9, 9, 9, 8, 8, 8, 9, 9, 8, 8, 9, 8, 9, 9, 9, 8, 8])
In [2]:np.random.randint(2,size = 20)
Out[2]:
array([1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1])
In [3]:random.randint(2,size = 20)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-102-d8e157c72f76> in <module>()
----> 1 random.randint(2,size = 20)

TypeError: randint() got an unexpected keyword argument 'size'
In [4]:random.randint(2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-104-7f7ff77c38fe> in <module>()
----> 1 random.randint(2)

TypeError: randint() takes exactly 3 arguments (2 given)
In [5]:random.randint(2,3)
Out[5]:
3

注意np.random.randint(a,b,size=c)与random.randint(a,b)的区别:

  1. random.randint(a,b)中得到的数字范围为a<=x=
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: np.random.randint是numpy库的一个函数,用于生成随机整数。它的参数包括low、high、size和dtype等,其low表示生成随机整数的下界,high表示生成随机整数的上界,size表示生成随机整数的形状,dtype表示生成随机整数的数据类型。使用np.random.randint函数可以方便地生成指定范围内的随机整数。 ### 回答2: np.random.randintPython一个用于生成随机整数的函数,它是Numpy模块的一个子模块。 np.random.randint函数的语法格式为: numpy.random.randint(low, high=None, size=None, dtype='l') 参数说明: 1. low:生成随机整数的最小值(包括该值)。 2. high:生成随机整数的最大值(不包括该值)。默认值为None,表示生成的随机整数在[0, high)之间。 3. size:生成随机整数的数量和形状。可以是整数、元组或者ndarray。默认值为None,表示返回一个随机整数。 4. dtype:生成随机整数的数据类型。默认值为‘l’,即生成有符号的long类型整数。 示例代码: import numpy as np # 生成一个随机整数 random_int = np.random.randint(10) print(random_int) # 输出一个0到9之间的随机整数 # 生成一个随机整数数组 random_int_array = np.random.randint(1, 10, size=(2, 3)) print(random_int_array) # 输出一个2*3的二维数组,数组的元素为1到9之间的随机整数 # 生成一个随机浮点数数组 random_float_array = np.random.randint(1, 10, size=(2, 3)).astype(np.float) print(random_float_array) # 输出一个2*3的二维数组,数组的元素为1到9之间的随机浮点数 总结: np.random.randintPython一个非常强大的随机整数生成函数,可以根据需要生成不同形状和数量的随机整数数组,是数据分析、机器学习、人工智能等领域必不可少的函数之一。 ### 回答3: np.random.randintPython的一个随机数生成函数,用于生成指定大小和范围的随机整数。该函数是NumPy的函数,使用前需先导入NumPy库,即import numpy as np。 函数定义: numpy.random.randint(low, high=None, size=None, dtype='l') 参数解释: low:生成的随机数包含low,其值为最小数。为必填参数。 high:生成的随机数不包含high,其值为最大数。为可选参数,默认值为None。 size:指定生成的随机整数的维度,形如(a,b,c)等,为可选参数,默认值为None。 dtype:生成的随机整数类型,如果不指定则默认为int64类型,为可选参数。 返回值: 返回值为指定大小和范围内的随机整数,以ndarray数组的形式返回。 举个例子:若想要生成一个5*5的随机整数数组,数值范围在0到10之间,那么可以使用以下代码: import numpy as np random_array = np.random.randint(0, 10, (5, 5)) 上述代码,第一个参数为0表示随机整数生成的最小值为0,第二个参数为10表示生成的最大整数数为10,第三个参数为(5,5)表示生成的随机整数数组的大小为5行5列。 总之,np.random.randint函数是Python的一个广泛应用的随机数生成函数,通过其强大的随机数生成能力和灵活的参数设置能够帮助用户生成各种类型的随机数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值