方法
random.RandomState.
uniform
从均匀分布中提取样品。
样品在半开放区间内均匀分布。 [low, high) (包括低,但不包括高)。换言之,给定间隔内的任何值同样可能被 uniform .
[low, high)
注解
新代码应该使用 uniform A方法 default_rng() 请参阅 快速启动 .
default_rng()
输出间隔的下限。生成的所有值都将大于或等于“低”。默认值为0。
输出间隔的上边界。生成的所有值都将小于或等于高。默认值为1.0。
输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。如果尺寸是 None (默认),如果 low 和 high 都是标量。否则, np.broadcast(low, high).size 取样。
(m, n, k)
m * n * k
None
low
high
np.broadcast(low, high).size
从参数化均匀分布中提取样本。
参见
randint
离散均匀分布,产生整数。
random_integers
闭区间上的离散均匀分布 [low, high] .
[low, high]
random_sample
浮体均匀分布 [0, 1) .
[0, 1)
random
Alias random_sample .
rand
接受尺寸作为输入的便利功能,例如, rand(2,2) 将生成一个2乘2的浮动数组,均匀分布在 [0, 1) .
rand(2,2)
Generator.uniform
应该用于新代码。
笔记
均匀分布的概率密度函数是
System Message: WARNING/2 (p(x)=\frac 1 b-a)
latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}p(x)=\frac 1 b-a\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}p(x)=\frac 1 b-a\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}p(x)=\frac 1 b-a\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}p(x)=\frac 1 b-a\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 388 bytes). Transcript written on math.log.
间隔内的任何地方 [a, b) ,其他地方为零。
[a, b)
什么时候? high = low 值 low 将被退回。如果 high < low ,结果是官方未定义的,最终可能会产生错误,即当传递的参数满足该不等式条件时,不依赖此函数的行为。这个 high 由于公式中的浮点舍入,返回的浮点数组中可能包含limit low + (high-low) * random_sample() . 例如:
low + (high-low) * random_sample()
>>> x = np.float32(5*0.99999999) >>> x 5.0
实例
从分发中抽取样本:
>>> s = np.random.uniform(-1,0,1000)
所有值都在给定的间隔内:
>>> np.all(s >= -1) True >>> np.all(s < 0) True
显示样本的直方图,以及概率密度函数:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(s, 15, density=True) >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') >>> plt.show()