import random # Initialiser le générteur de nombres aléatoires random.seed(42) # Tirer un nombre aléatoire # Tirer un nombre entre 0 et 1 print(random.random()) # Tirer un nombre entier compris dans un intervalle print(random.randint(1, 20)) # Tirer un nombre dans une série print(random.randrange(0, 101, 2)) # Loi normale print(random.gauss(mu=0, sigma=1)) # Loi uniforme print(random.uniform(a=2, b=5)) # Loi exponentielle print(random.expovariate(lambd=1/5)) 0.6394267984578837 1 94 -0.11131586156766246 4.209413642492037 5.645865043992964 # Tirer un échantillon colors = ['Green', 'Yellow', 'Black', 'Blue', 'White', 'Pink', 'Red'] # Tirage aléatoire d'un élément print(random.choice(colors)) # Tirage avec remise print(random.choices(colors, k=10)) # Tirage sans remise print(random.sample(colors, k=5)) White ['Green', 'Black', 'Green', 'Yellow', 'Blue', 'Green', 'Yellow', 'White', 'Blue', 'Yellow'] ['White', 'Black', 'Green', 'Yellow', 'Pink'] # Mélanger les éléments d'une liste colors = ['Green', 'Yellow', 'Black', 'Blue', 'White', 'Pink', 'Red'] print(colors) random.shuffle(colors) print(colors) ['Green', 'Yellow', 'Black', 'Blue', 'White', 'Pink', 'Red'] ['White', 'Red', 'Green', 'Yellow', 'Pink', 'Black', 'Blue'] Pour en savoir plus, merci de consulter la documentation officielle de Python.