标签:20 randint random high print 随机数 np
先来看看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 #期望结果的类型
标签:20,randint,random,high,print,随机数,np 来源: https://www.cnblogs.com/insane-Mr-Li/p/13266145.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。