How can I generate random numbers in Python?

后端 未结 2 1673
囚心锁ツ
囚心锁ツ 2021-02-05 16:41

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

  • Normal
  • Poisson
  • Exp
相关标签:
2条回答
  • 2021-02-05 17:24
    #!/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.

    0 讨论(0)
  • 2021-02-05 17:44

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

    Numpy.random would be the next place to look.

    0 讨论(0)
提交回复
热议问题