Random number in Python

A brief note about random number in Python
For this purpose, we need import random module.
random.random()用於生成一個0到1的隨機符點數: 0 <= n < 1.0

import random
random.random()

0.14812154696888702

random.random(a,b)用來產生介於a與b之間的整數

random.randint(12, 20)

14

random.uniform(a,b)用來產生介於a與b之間的浮點數

random.uniform(12,20)

14.255325776146638

上述函數其實也就是Uniform distribution, 而Python當中還提供了相當多的統計分配:

  • uniform
  • triangular
  • normal (Gaussian)
  • lognormal
  • negative exponential
  • gamma
  • beta
  • pareto
  • Weibull

例如常見的常態分配, gauss(\mu, \sigma)

random.gauss(5,1)

6.040336918401728

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s