如何在 Python 中生成随机数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1655559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:48:33  来源:igfitidea点击:

How can I generate random numbers in Python?

pythonrandom

提问by Nathan Fellman

Are there any built-in libraries in Python or Numpy to generate random numbers based on various common distributions, such as:

Python 或 Numpy 中是否有任何内置库可以根据各种常见分布生成随机数,例如:

  • Normal
  • Poisson
  • Exponential
  • Bernoulli
  • 普通的
  • 泊松
  • 指数
  • 伯努利

And various others?

和其他各种?

Are there any such libraries with multi-variate distributions?

有没有这样的具有多变量分布的库?

回答by unutbu

#!/usr/bin/env python
from scipy.stats import bernoulli,poisson,norm,expon

bernoulli, poisson, norm, expon and many others are documented here

伯努利、泊松、范数、指数和许多其他的记录在这里

print(norm.rvs(size=30))
print(bernoulli.rvs(.3,size=30))
print(poisson.rvs(1,2,size=30))
print(expon.rvs(5,size=30))

All the distributions defined in scipy.stats have a common interface to the pdf, cdf, rvs (random variates). More info here.

scipy.stats 中定义的所有分布都有一个通用的 pdf、cdf、rvs(随机变量)接口。更多信息在这里

回答by Ned Batchelder

The randommodule has tons of functions for generating random numbers in lots of way. Not sure it has multi-variate.

random模块具有大量用于以多种方式生成随机数的功能。不确定它是否具有多变量。

Numpy.randomwould be the next place to look.

Numpy.random将是下一个查看的地方。