ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python中random(numpy.random)随机数的使用

2021-12-18 19:03:31  阅读:317  来源: 互联网

标签:Draw python random sig samples distribution numpy size


基础知识(maybe is boring,but it's fundamental):

(一)random

(1)实值分布

random.random()

  返回 [0.0, 1.0) 范围内的下一个随机浮点数。

random.uniform(a, b)

  返回一个随机浮点数 N ,当 a <= b 时 a <= N <= b ,当 b < a 时 b <= N <= a 。

取决于等式 a + (b-a) * random() 中的浮点舍入,终点 b 可以包括或不包括在该范围内。

random.triangular(low, high, mode)

  返回一个随机浮点数 N ,使得 low <= N <= high 并在这些边界之间使用指定的 mode 。 low 和 high 边界默认为零和一。 mode 参数默认为边界之间的中点,给出对称分布。

random.betavariate(alpha, beta)

  Beta 分布。 参数的条件是 alpha > 0 和 beta > 0。 返回值的范围介于 0 和 1 之间。

random.expovariate(lambd)

  指数分布。 lambd 是 1.0 除以所需的平均值,它应该是非零的。 (该参数本应命名为 “lambda” ,但这是 Python 中的保留字。)如果 lambd 为正,则返回值的范围为 0 到正无穷大;如果 lambd 为负,则返回值从负无穷大到 0。

random.gammavariate(alpha, beta)

  Gamma 分布。 ( 不是 gamma 函数! ) 参数的条件是 alpha > 0 和 beta > 0

random.gauss(mu, sigma)

  高斯分布。 mu 是平均值,sigma 是标准差。 这比下面定义的 normalvariate() 函数略快。

random.lognormvariate(mu, sigma)

  对数正态分布。 如果你采用这个分布的自然对数,你将得到一个正态分布,平均值为 mu 和标准差为 sigma 。 mu 可以是任何值,sigma 必须大于零。

random.normalvariate(mu, sigma)

  正态分布。 mu 是平均值,sigma 是标准差。

random.vonmisesvariate(mu, kappa)

  冯·米塞斯分布。 mu 是平均角度,以弧度表示,介于0和 2*pi 之间,kappa 是浓度参数,必须大于或等于零。 如果 kappa 等于零,则该分布在 0 到 2*pi 的范围内减小到均匀的随机角度。

random.paretovariate(alpha)

  帕累托分布。 alpha 是形状参数。

random.weibullvariate(alpha, beta)

  威布尔分布。 alpha 是比例参数,beta 是形状参数。

(2)序列用函数

random.choice(seq)

  从非空序列 seq 返回一个随机元素。

random.shuffle(x[, random])

  将序列 x 随机打乱位置。

random.sample(population, k)

  返回从总体序列或集合中选择的唯一元素的 k 长度列表。 用于无重复的随机抽样。

(3)整数用函数

random.randrange(stop)

random.randrange(start, stop[, step])

从 range(start, stop, step) 返回一个随机选择的元素。 这相当于 choice(range(start, stop, step)) ,但实际上并没有构建一个 range 对象。

random.randint(a, b)

返回随机整数 N 满足 a <= N <= b。相当于 randrange(a, b+1)。

 random.seed()用法

当seed()没有参数时,每次生成的随机数是不一样的,而当seed()有参数时,每次生成的随机数是一样的,同时选择不同的参数生成的随机数也不一样。

(二)numpy

beta(a, b[, size])

Draw samples from a Beta distribution.  Beta分布

binomial(n, p[, size])

Draw samples from a binomial distribution.  二项分布  

bytes(length)

Return random bytes.  随机字节

chisquare(df[, size])

Draw samples from a chi-square distribution.

choice(a[, size, replace, p])

Generates a random sample from a given 1-D array   一维数组生成随机样本

dirichlet(alpha[, size])

Draw samples from the Dirichlet distribution.  狄里克莱分布

exponential([scale, size])

Draw samples from an exponential distribution.

f(dfnum, dfden[, size])

Draw samples from an F distribution.  指数分布

gamma(shape[, scale, size])

Draw samples from a Gamma distribution.

geometric(p[, size])

Draw samples from the geometric distribution.  几何分布

get_state()

Return a tuple representing the internal state of the generator.

gumbel([loc, scale, size])

Draw samples from a Gumbel distribution.

hypergeometric(ngood, nbad, nsample[, size])

Draw samples from a Hypergeometric distribution.

laplace([loc, scale, size])

Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay).

logistic([loc, scale, size])

Draw samples from a logistic distribution.  

lognormal([mean, sigma, size])

Draw samples from a log-normal distribution.  对数正态分布

logseries(p[, size])

Draw samples from a logarithmic series distribution.

multinomial(n, pvals[, size])

Draw samples from a multinomial distribution.

multivariate_normal(mean, cov[, size, …])

Draw random samples from a multivariate normal distribution.

negative_binomial(n, p[, size])

Draw samples from a negative binomial distribution.

noncentral_chisquare(df, nonc[, size])

Draw samples from a noncentral chi-square distribution.

noncentral_f(dfnum, dfden, nonc[, size])

Draw samples from the noncentral F distribution.

normal([loc, scale, size])

Draw random samples from a normal (Gaussian) distribution.  正态(高斯)分布

pareto(a[, size])

Draw samples from a Pareto II or Lomax distribution with specified shape.

permutation(x)

Randomly permute a sequence, or return a permuted range.

poisson([lam, size])

Draw samples from a Poisson distribution.  泊松分布

power(a[, size])

Draws samples in [0, 1] from a power distribution with positive exponent a - 1.

rand(d0, d1, …, dn)

Random values in a given shape.

randint(low[, high, size, dtype])

Return random integers from low (inclusive) to high (exclusive).  “标准正态”分布

randn(d0, d1, …, dn)

Return a sample (or samples) from the “standard normal” distribution.

random([size])

Return random floats in the half-open interval [0.0, 1.0).

random_integers(low[, high, size])

Random integers of type np.int_ between low and high, inclusive.

random_sample([size])

Return random floats in the half-open interval [0.0, 1.0).

ranf()

This is an alias of random_sample.

rayleigh([scale, size])

Draw samples from a Rayleigh distribution.

sample()

This is an alias of random_sample.

seed(self[, seed])

Reseed a legacy MT19937 BitGenerator

set_state(state)

Set the internal state of the generator from a tuple.

shuffle(x)

Modify a sequence in-place by shuffling its contents.

standard_cauchy([size])

Draw samples from a standard Cauchy distribution with mode = 0.  标准指数

standard_exponential([size])

Draw samples from the standard exponential distribution.

standard_gamma(shape[, size])

Draw samples from a standard Gamma distribution.

standard_normal([size])

Draw samples from a standard Normal distribution (mean=0, stdev=1).  标准正态分布

standard_t(df[, size])

Draw samples from a standard Student’s t distribution with df degrees of freedom.

triangular(left, mode, right[, size])

Draw samples from the triangular distribution over the interval [left, right].

uniform([low, high, size])

Draw samples from a uniform distribution.

vonmises(mu, kappa[, size])

Draw samples from a von Mises distribution.

wald(mean, scale[, size])

Draw samples from a Wald, or inverse Gaussian, distribution.

weibull(a[, size])

Draw samples from a Weibull distribution.

zipf(a[, size])

Draw samples from a Zipf distribution.

(三)作图实例

高斯正态分布

import numpy as np
import matplotlib.pyplot as plt
import math
u = 8782  # 均值μ
sig = math.sqrt(2800*2800) # 标准差δ
x = np.linspace(u - 3*sig, u + 3*sig, 500000)
y_sig = np.exp(-(x - u) ** 2 /(2* sig **2))/(math.sqrt(2*math.pi)*sig)
print(x)
print(len(x))
print("="*20)
print(y_sig)
plt.plot(x, y_sig, "r-", linewidth=2)
plt.grid(True)
plt.show()

对数正太分布

import numpy as np
import matplotlib.pyplot as plt
import math,sys
#np.seterr(divide = 'ignore')
u = 0  # 均值μ
sig =2/3 # 标准差δ
x = np.linspace(0.0001,10,100000)
y_sig = np.exp(-(np.log(x)-u) ** 2 /(2* sig **2))/(math.sqrt(2*math.pi)*sig*x)
print(x)
print(x*8000)
print(len(x))
print("="*20)
print(y_sig)
plt.plot(x*8000, y_sig, "r-", linewidth=2)
plt.grid(True)
plt.show()

import numpy as np #导入库
random3 = np.random.randn(10000) #随机生成10000个服从正态分布的随机数
print(random3*8000)
print(len([a for a in random3*8000 if a<8000]))
import matplotlib.pyplot as plt
import seaborn as sns #使用seaborn 库画直方图验证结果
sns.set_palette("hls") #设置所有图的颜色,使用hls色彩空间
sns.distplot(random3*8000,color="r",bins=4000,kde=True) #绘制直方图,color设置颜色,bins设置直方图的划分数
plt.show() #显示验证结果

import numpy as np #导入库
random3 = np.random.lognormal(0,1/2,500000) #随机生成服从对数正态分布的随机数
print(random3*8000)
print(len([a for a in random3*8000 if a<8000]))
import matplotlib.pyplot as plt
import seaborn as sns #使用seaborn 库画直方图验证结果
sns.set_palette("hls") #设置所有图的颜色,使用hls色彩空间
sns.distplot(random3*8000,color="r",bins=4000,kde=True) #绘制直方图,color设置颜色,bins设置直方图的划分数
plt.show() #显示验证结果

标签:Draw,python,random,sig,samples,distribution,numpy,size
来源: https://www.cnblogs.com/kuhns/p/15704601.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有