from pylab import *
help(randint)
Help on built-in function randint: randint(...) randint(low, high=None, size=None) Return random integers from `low` (inclusive) to `high` (exclusive). Return random integers from the "discrete uniform" distribution in the "half-open" interval [`low`, `high`). If `high` is None (the default), then results are from [0, `low`). Parameters ---------- low : int Lowest (signed) integer to be drawn from the distribution (unless ``high=None``, in which case this parameter is the *highest* such integer). high : int, optional If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if ``high=None``). size : int or tuple of ints, optional Output shape. Default is None, in which case a single int is returned. Returns ------- out : int or ndarray of ints `size`-shaped array of random integers from the appropriate distribution, or a single such random int if `size` not provided. See Also -------- random.random_integers : similar to `randint`, only for the closed interval [`low`, `high`], and 1 is the lowest value if `high` is omitted. In particular, this other one is the one to use to generate uniformly distributed discrete non-integers. Examples -------- >>> np.random.randint(2, size=10) array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) >>> np.random.randint(1, size=10) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) Generate a 2 x 4 array of ints between 0 and 4, inclusive: >>> np.random.randint(5, size=(2, 4)) array([[4, 0, 2, 1], [3, 2, 2, 0]])
def dice():
return randint(1, 7)
for i in range(0, 20):
r = dice()
print r,
print
1 2 2 4 3 5 1 2 1 2 6 6 1 5 2 6 2 3 6 2
def f(k):
s = 0
for i in range(0, k):
s = s + dice()
return s
l = [] # liste des résulats
for i in range(0,100):
l.append(f(5))
print l
print
print "moyenne théorique = ", 5 * (7.0/2.0) # attention à ne pas utiliser la division entière.
print "moyenne empirique = ", mean(l)
print
print "écart type théorique = ", sqrt(5) * sqrt(35.0/12.0) # attention à ne pas utiliser la division entière.
print "écart type empirique = ", std(l)
[14, 22, 22, 17, 15, 15, 19, 23, 26, 22, 27, 17, 16, 16, 17, 26, 18, 17, 20, 25, 24, 17, 21, 16, 19, 15, 18, 15, 21, 21, 17, 25, 16, 14, 16, 21, 16, 22, 14, 16, 14, 17, 20, 15, 20, 13, 16, 20, 18, 15, 19, 14, 24, 15, 20, 17, 24, 20, 15, 21, 23, 22, 17, 21, 19, 19, 19, 15, 22, 20, 19, 15, 13, 17, 19, 17, 16, 19, 15, 12, 21, 20, 21, 19, 15, 15, 13, 18, 22, 21, 14, 10, 16, 18, 14, 17, 22, 16, 21, 25] moyenne théorique = 17.5 moyenne empirique = 18.32 écart type théorique = 3.81881307913 écart type empirique = 3.49822812292
res = arange(4.5, 31)
hist(l, res, normed=True)
xs = np.linspace(0, 35, 256, endpoint=True)
plot(xs, mlab.normpdf(xs, 17.5, 3.819), linestyle='-', color='red', linewidth=3)
show()
def test(k, n):
l = [] # liste des résulats
# generation des valeurs
for i in range(0,n):
l.append(f(k))
# valeurs théoriques
mu = k * 3.5
sigma = sqrt(k * 35.0/12.0)
# tracé de l'histogramme
xs = arange(k-0.5, 6*k+1) # on décale de -0.5 pour avoir la barre correspondant
# à une somme s fixée centrée par rapport à x=s
hist(l, xs, normed=True, color='cyan')
# tracé de la Gaussienne théorique
xs = linspace(0, 7*k, 256, endpoint=True)
plot(xs, mlab.normpdf(xs, mu, sigma), color='red', linewidth=3)
# tracé de la Gaussienne empirique
plot(xs, mlab.normpdf(xs, mean(l), std(l)), color='green', linewidth=3, linestyle="--")
# petit test
test(5, 100)