Random module


In [ ]:
from random import seed, randrange

seed()
for i in range(20):
    print('{:<4d}'.format(randrange(0, 100, 5)), end = '')
print()
for n in range(2):
    print()
    seed(0)
    for i in range(20):
        print('{:<4d}'.format(randrange(0, 100, 5)), end = '')
    print()
    seed(1)
    for i in range(20):
        print('{:<4d}'.format(randrange(0, 100, 5)), end = '')
    print()

In [ ]:
%matplotlib inline
from matplotlib.pyplot import bar, xticks

from random import randint, randrange, choice

counts = [0] * 10
for i in range(100):
    counts[randint(0, 9)] += 1
bar(range(10), counts, 0.2)
counts = [0] * 10
for i in range(100):
    counts[randrange(0, 10)] += 1
bar([0.2 + i for i in range(10)], counts, 0.2, color = 'r')
counts = [0] * 10
for i in range(100):
    counts[choice(range(0, 10))] += 1
bar([0.4 + i for i in range(10)], counts, 0.2, color = 'y')
xticks([i + 0.4 for i in range(10)], range(0, 10))
print()

In [ ]:
from random import random, getstate, setstate

for i in range(5):
    print(random())
state = getstate()
print()

for i in range(5):
    print(random())
print()

setstate(state)
for i in range(5):
    print(random())

In [ ]:
from random import getrandbits

for k in range(1, 6):
    for i in range(20):
        print('{:<4d}'.format(getrandbits(k)), end = '')
    print()

In [ ]:
from random import shuffle

L = list(range(6))

for i in range(10):
    shuffle(L)
    print(L)

In [ ]:
from random import sample

for k in range(1, 6):
    for i in range(5):
        print(sample(range(100), k))
    print()

Uniform density function $\frac{x}{b - a}$ over $[a,b]$ illustrated with $a=-1$ and $b=1$


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import uniform

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(uniform(-1, 1))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 20)
print()

Normal density function $\frac{1}{\sqrt(2\pi\sigma)}e^{\frac{-(x-\mu)^2}{2\sigma^2}}$ illustrated with $\mu=15$ and $\sigma=8$


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import gauss
# Alternatively: from random import normalvariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(gauss(15, 8))
# Alternatively:    generated_values.append(normalvariate(15, 8))
  
def to_percent(x, position):
    return str(x / nb_of_values * 100) + '%'

#gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 100)
print()

Log normal density function, that is, the density function of the random variable $e^X$ where $X$ is a normal random variable, illustrated with $\mu_X=10000$ and $\sigma_X=2$


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca, semilogx
from matplotlib.ticker import FuncFormatter

from random import lognormvariate
from math import exp, log

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(lognormvariate(log(10000), 2))
minimal_value = min(generated_values)
maximal_value = max(generated_values)

def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().set_xscale('log')
gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, [exp((log(maximal_value) - log(minimal_value)) * i / 100)
                        for i in range(101)])
print()

Exponential density function $\lambda e^{-\lambda x}$ illustrated with $\lambda=7$


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca, text
from matplotlib.ticker import FuncFormatter

from random import expovariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    # Normal distribution of mean 15 and standard deviation 8
    generated_values.append(expovariate(7))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 50)
text(1, 10000, 'Mean: $\\frac{1}{7}\\approx 0.14$')
print()

Given $a\leq b$ and $c\in[a,b]$ (the mode), the density function of the triangular distribution over $[a,b]$ is equal to:

  • $\frac{2(x-a)}{(b-a)(c-a)}$ if $a<x<c$,
  • $\frac{2}{b-a}$ if $x=c$,
  • $\frac{2(b-x)}{(b-a)(b-c)}$ if $c<x<b$.

It is illustrated with $a=1$, $b=4$ and $c=3$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import triangular

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(triangular(1, 4, 3))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 20)
print()

Given $\alpha>0$, the density function of the Pareto distribution of parameter $\alpha$, defined over $[1,\infty)$, is equal to $\frac{\alpha}{x^{\alpha+1}}$. It is illustrated with $\alpha=10000$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import paretovariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(paretovariate(10000))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

def literal(x, _):
    return '{:.4f}'.format(x)

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
gca().xaxis.set_major_formatter(FuncFormatter(literal))

hist(generated_values, 50)
print()

Given $\alpha>0$ and $\beta>0$, the density function of the beta distribution of parameters $\alpha$ and $\beta$, defined over $[0,1]$, is equal to $\frac{x^{\alpha-1}(1-x)^{\beta-1}}{\int_0^1x^{\alpha-1}(1-x)^{\beta-1}dx}$. It is illustrated with $\alpha=4$ and $\beta=2.5$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import betavariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(betavariate(4, 2.5))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 50)
print()

Given $\alpha>0$ and $\beta>0$, the density function of the gamma distribution of parameters $\alpha$ and $\beta$, defined over $(0,\infty)$, is equal to $\frac{\beta^\alpha}{\Gamma(\alpha)}x^{\alpha-1}e^{-\beta x}$. It is illustrated with $\alpha=4$ and $\beta=2.5$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import gammavariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(gammavariate(4, 2.5))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 50)
print()

Given $\mu\in[0,2\pi)$ and $\kappa>0$, the density function of the von Mises (or circular normal, or Tikhonov) distribution of parameters $\mu$ and $\kappa$, defined over $[0,2\pi]$, is equal to $\frac{e^{\kappa\cos(x-\mu)}}{2\pi\Sigma_{m=0}^\infty\frac{1}{m!\Gamma(m+\alpha+1)}(\frac{x}{2})^{2m+\alpha}}$. It is illustrated with $\mu=\pi$ and $\kappa=4$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, xlim, gca
from matplotlib.ticker import FuncFormatter

from random import vonmisesvariate
from math import pi

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(vonmisesvariate(pi, 4))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

xlim(0, 2 * pi)
gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 50)
print()

Given $\lambda>0$ and $\kappa>0$, the density function of the Weibull distribution of parameters $\lambda$ and $\kappa$, defined over $[0,\infty)$, is equal to $\frac{\kappa}{\lambda}(\frac{x}{\lambda})^{k-1}e^{-(\frac{x}{\lambda})^\kappa}$. It is illustrated with $\lambda=1$ and $\kappa=1.5$.


In [ ]:
%matplotlib inline
from matplotlib.pyplot import hist, gca
from matplotlib.ticker import FuncFormatter

from random import weibullvariate

nb_of_values = 100000
generated_values = []
for i in range(nb_of_values):
    generated_values.append(weibullvariate(1, 1.5))
    
def to_percent(x, _):
    return str(x / nb_of_values * 100) + '%'

gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
hist(generated_values, 50)
print()