As you only return h
, you don’t actually need t
. It is also not needed to use randrange
— you can achieve the same with random
.
For the actual counting you can use sum
, and the boolean result can be converted to 0 or 1 with int
:
def prob(times):
return sum(int(rd.random() < 0.7) for _ in range(times))
Or in lambda notation:
prob = lambda times: sum(int(rd.random() < 0.7) for _ in range(times))
2
solved Is there a shorter version, biased probability?