Python基础----随机数

Python中的random模块

random模块中最常用的几个函数:

   random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0   [0,1)。
   
   random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a < b,则生成的随机数n: a <= n <= b。如果 a >b, 则 b <= n <= a。
   
   random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b。
   
   random.randrange的函数原型为:random.randrange([start], stop[, step]),从指定范围内,按指定基数递增的集合中 获取一个随机数。
   
   random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。
   
   random.shuffle的函数原型为:random.shuffle(x[, random]),用于将一个列表中的元素打乱。
   
   random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
import random
a = random.random()
print (a)
b = random.uniform(10.0,15.0)
print (b)
bb = random.uniform(15.0,10.0)
print (bb)
c = random.randint(1,7)
print (c)
d = random.randrange(1,10,2)
print (d)
e = random.choice(range(0,10,2))
print (e)
f = list('difference')
print (f)
random.shuffle(f)
print (f)
g = random.sample(f,4)
print (g)
print (f)
0.7557760631464904
11.976117650486488
10.624028534361265
1
5
2
['d', 'i', 'f', 'f', 'e', 'r', 'e', 'n', 'c', 'e']
['d', 'i', 'r', 'e', 'e', 'n', 'e', 'f', 'c', 'f']
['e', 'e', 'r', 'f']
['d', 'i', 'r', 'e', 'e', 'n', 'e', 'f', 'c', 'f']

Numpy中的随机数

下面内容引用自链接博客,感觉他总结的非常不错。https://blog.csdn.net/jinxiaonian11/article/details/53143141

numpy.random模块分为四个部分,对应四种功能:

  1. 生成器:种随机数种子,根据同一种子产生的随机数是相同的
  2. 简单随机数: 产生简单的随机数据,可以是任何维度
  3. 分布:产生指定分布的数据,如高斯分布等
  4. 排列:将所给对象随机排列

1. 生成器

电脑产生随机数需要明白以下几点:

(1)随机数是由随机种子根据一定的计算方法计算出来的数值。所以,只要计算方法一定,随机种子一定,那么产生的随机数就不会变。

(2)只要用户不设置随机种子,那么在默认情况下随机种子来自系统时钟(即定时/计数器的值)

(3)随机数产生的算法与系统有关,Windows和Linux是不同的,也就是说,即便是随机种子一样,不同系统产生的随机数也不一样。

numpy.random 设置种子的方法有:

函数名称 函数功能 参数说明
RandomState 定义种子类 RandomState是一个种子类,提供了各种种子方法,最常用seed
seed([seed]) 定义全局种子 参数为整数或者矩阵
import numpy as np
np.random.seed(1234)

#只显示小数点后两位
np.set_printoptions(precision = 2)

2. 简单随机数

函数名称 函数功能 参数说明
randint(low[, high, size, dtype]) 产生随机整数 low:最小值;high:最大值;size:数据个数
random_sample([size]) 在[0,1)内产生随机数 size:随机数的shape,可以为元祖或者列表,[2,3]表示2维随机数,维度为(2,3)
random([size]) 同random_sample([size]) 同random_sample([size])
ranf([size]) 同random_sample([size]) 同random_sample([size])
sample([size])) 同random_sample([size]) 同random_sample([size])
choice(a[, size, replace, p]) 从a中随机选择指定数据 a:1维数组 size:返回数据形状
bytes(length) 返回随机位 length:位的长度
rand(d0, d1, …, dn) 产生 [0,1)之间均匀分布的随机数 dn为第n维数据的维度
randn(d0, d1, …, dn) 产生标准正态分布随机数 dn为第n维数据的维度
# 指定范围内的随机整数
a = np.random.randint(1,5,10)
print (a)
aa = np.random.randint(1,5,[2,5])
print (aa)
# [0,1)内随机数
b = np.random.random_sample(10)
print (b)
bb = np.random.random_sample([2,5])
print (bb)
c = np.random.random(10)
print (c)
d = np.random.sample(10)
print (d)
e = np.random.ranf(10)
print (e)
f = np.random.choice(5,10)
print (f)
g = np.random.bytes(10)
print (g)
h = np.random.rand(2,3)  #[0,1)均匀分布里面取数
print (h)
i = np.random.randn(2,3) #标准正太分布里面取数
print (i)
[3 1 1 3 4 2 1 3 2 3]
[[4 1 2 4 1]
 [2 2 2 3 1]]
[0.72 0.87 0.13 0.97 0.75 0.92 0.04 0.29 0.82 0.27]
[[0.05 0.15 0.02 0.78 0.35]
 [0.75 0.05 0.08 0.79 0.37]]
[0.11 0.41 0.65 0.44 0.2  0.44 0.74 0.1  0.51 0.84]
[0.99 0.63 0.29 0.13 0.01 0.34 0.48 0.55 0.04 0.06]
[0.02 0.51 0.27 0.45 0.97 0.8  0.28 0.56 0.2  0.79]
[4 4 1 1 2 2 1 3 2 3]
b'\xf0\x15\xad\x02\xb3]\x05\xd5\xc6\xbf'
[[0.89 0.69 0.46]
 [0.48 0.33 0.27]]
[[ 0.5  -1.04  1.78]
 [ 0.73  0.25 -0.55]]

3. 分布

numpy.random模块提供了产生各种分布随机数的API:

函数名称 函数功能
beta(a, b[, size]) 贝塔分布样本,在 [0, 1]内
binomial(n, p[, size]) 二项分布的样本
chisquare(df[, size]) 卡方分布样本
dirichlet(alpha[, size]) 狄利克雷分布样本
exponential([scale, size]) 指数分布
f(dfnum, dfden[, size]) F分布样本
gamma(shape[, scale, size]) 伽马分布
geometric(p[, size]) 几何分布
gumbel([loc, scale, size]) 耿贝尔分布
hypergeometric(ngood, nbad, nsample[, size]) 超几何分布样本
laplace([loc, scale, size]) 拉普拉斯或双指数分布样本
logistic([loc, scale, size]) Logistic分布样本
lognormal([mean, sigma, size]) 对数正态分布
logseries(p[, size]) 对数级数分布
multinomial(n, pvals[, size]) 多项分布
multivariate_normal(mean, cov[, size]) 多元正态分布
negative_binomial(n, p[, size]) 负二项分布
noncentral_chisquare(df, nonc[, size]) 非中心卡方分布
noncentral_f(dfnum, dfden, nonc[, size]) 非中心F分布
normal([loc, scale, size]) 正态(高斯)分布
pareto(a[, size]) 帕累托(Lomax)分布
poisson([lam, size]) 泊松分布
power(a[, size]) Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
rayleigh([scale, size]) Rayleigh 分布
standard_cauchy([size]) 标准柯西分布
standard_exponential([size]) 标准的指数分布
standard_gamma(shape[, size]) 标准伽马分布
standard_normal([size]) 标准正态分布 (mean=0, stdev=1).
standard_t(df[, size]) Standard Student’s t distribution with df degrees of freedom.
triangular(left, mode, right[, size]) 三角形分布
uniform([low, high, size]) 均匀分布
vonmises(mu, kappa[, size]) von Mises分布
wald(mean, scale[, size]) 瓦尔德(逆高斯)分布
weibull(a[, size]) Weibull 分布
zipf(a[, size]) 齐普夫分布
h = np.random.rand(2,3)  #[0,1)均匀分布里面取数
print (h)
hh= np.random.uniform(2,3,10)  #均匀分布里面取数
print (hh)
hh= np.random.uniform(2,3,[2,3])  #均匀分布里面取数
print (hh)
i = np.random.randn(2,3) #标准正太分布里面取数
print (i)
ii = np.random.normal(2,3,[2,3]) #正太分布里面取数
print (ii)
[[0.06 0.25 0.95]
 [0.22 0.96 0.2 ]]
[2.34 2.82 2.85 2.57 2.9  2.51 2.21 2.86 2.01 2.62]
[[2.02 2.9  2.16]
 [2.47 2.77 2.8 ]]
[[ 0.1   0.56 -0.04]
 [-1.06 -0.14  1.15]]
[[ 4.15  0.49 -3.74]
 [ 1.22  7.03 -2.65]]

4. 排列

函数名称 函数功能 参数说明
shuffle(x) 打乱对象x(多维矩阵按照第一维打乱) 矩阵或者列表
permutation(x) 打乱并返回该对象(多维矩阵按照第一维打乱) 整数或者矩阵
j = np.random.randint(1, 10, (3, 4))
print(j)
np.random.shuffle(j)
print(j)
jj = np.random.permutation(j)
print (jj)
print (j)
[[3 9 1 8]
 [5 7 1 5]
 [9 1 2 7]]
[[3 9 1 8]
 [9 1 2 7]
 [5 7 1 5]]
[[9 1 2 7]
 [5 7 1 5]
 [3 9 1 8]]
[[3 9 1 8]
 [9 1 2 7]
 [5 7 1 5]]

猜你喜欢

转载自blog.csdn.net/weixin_42432468/article/details/88651579