Random numbers

Drawing scalar random numbers:
import random
random.seed(2198)  # control the seed

u = random.random()           # uniform number on [0,1)
u = random.uniform(-1, 1)     # uniform number on [-1,1)
u = random.gauss(m, s)        # number from N(m,s)
Vectorized drawing of random numbers (arrays):
from numpy import random
random.seed(12)               # set seed

u = random.random(n)          # n uniform numbers on (0,1)
u = random.uniform(-1, 1, n)  # n uniform numbers on (-1,1)
u = random.normal(m, s, n)    # n numbers from N(m,s)
Note that both modules have the name random! A remedy:
import random as random_number   # rename random for scalars
from numpy import *              # random is now numpy.random

previousnexttable of contents