Générer des nombres entiers aléatoires entre 0 et 9

Comment puis-je générer des nombres entiers aléatoires compris entre 0 et 9 (inclus) en Python ?

Par exemple, 0',1', 2',3', 4',5', 6',7', 8',9'.

Solution

Essayez :

from random import randrange
print(randrange(10))

Plus d'informations : http://docs.python.org/library/random.html#random.randrange

Commentaires (1)
import random
print(random.randint(0,9))

random.randint(a, b)

Retourne un entier aléatoire N tel que a

Commentaires (0)

Essayez ça :

from random import randrange, uniform

# randrange gives you an integral value
irand = randrange(0, 10)

# uniform gives you a floating-point value
frand = uniform(0, 10)
Commentaires (0)