1. random.randint(a,b): 随机生成一个a到b之间的一个整数(a<=n<=b)
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b
2. random.uniform(a,b):随机生成一个a到b之间的一个浮点数(a<=n<=b)
用于生成一个指定范围内的随机浮点数,两格参数中,其中一个是上限,一个是下限。如果a>b,则生成的随机数n,即b<=n<=a;如果a>b,则a<=n<=b。
3. numpy.random.uniform(low,high,size):输出size个low到high之间的数(low<=n<size)左闭右开
从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
- low: 采样下界,float类型,默认值为0;
- high: 采样上界,float类型,默认值为1;
- size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出m*n*k个样本,缺省时输出1个值。
4. 例:
import random
import numpy as np
a=0; b=7
index = random.randint(a, b) #随机产生0到7之间的一个整数
print(index)
a=10; b=20
index = random.uniform(a,b) #随机产生10到20之间的一个浮点数
print(index)
shape=17
cond = np.random.uniform(-0.1, 0.1, shape) #随机产生-0.1到0.1之间的17个浮点数
print(cond)
#输出结果:
6
17.53995250541246
[ 0.08522486 -0.00211719 0.08933278 -0.00373567 0.06636708 0.0121164
0.06903136 0.05635546 -0.07668082 0.0551379 -0.07817495 0.03939773
-0.02098632 -0.07445089 0.0423184 -0.03277861 0.09023184]
#输出结果
4
15.57572931119358
[-0.07759161 0.00917166 0.02625699 -0.09831834 0.05971643 -0.09321461
-0.0589061 0.06328565 -0.05303397 -0.07837874 -0.09519935 0.00481752
0.09597165 0.08807344 -0.06250566 -0.07776539 0.00727126]
参考:
https://www.cnblogs.com/chamie/p/4917820.html python--随机函数(random,uniform,randint,randrange,shuffle,sample)
https://blog.csdn.net/u013920434/article/details/52507173 numpy.random.uniform介绍