Python Puzzles
In Probability Land, on a sunny day there is an equal probability of the next day being sunny or rainy. On a rainy day, there is a 70% chance it will rain the next day, and a 30% chance it will be sunny the next day.
On average, how many rainy days are there in Probability Land?
It rains approximately 62% of the time in Probability Land.
import numpy as np
result = []
for _ in range(10_000):
sunnydays = 0
forecast = np.random.binomial(1,.7,1).item()
for _ in range(365):
if forecast == 1:
sunnydays += 1
forecast = np.random.binomial(1,.5,1).item()
else:
forecast = np.random.binomial(1,.3,1).item()
result.append(sunnydays)
pct_rain = ((365 - (sum(result)/len(result)))/365)
print('It rains approximately ' + '{0:.0%}'.format(pct_rain) + ' of the time in ProbabilityLand.')