mercredi 26 mai 2021

np.random.binomial() vs random.choices() for simulating coin flips

I want to simulate repeated flips of a fair coin using Python. Two approaches seem reasonable, as shown in the code below. Are they equivalent please, and if so is there any good reason to prefer one over the other?

import random
import numpy as np

n = 10
p = 0.5

print(np.random.binomial(n, p))

results = random.choices([True, False], k=n)
print(results.count(True))



Aucun commentaire:

Enregistrer un commentaire