import numpy as np # %% Number of trials (experiments): n = int(1e5) # %% Params of choice(): sample_space = np.array(['Boy', 'Girl']) sample_size = 5 replace = True prob = np.array([105/205, 100/205]) # %% Variables holding results: n_event = 0 # %% For loop realizing the n trials (samplings): for k in range(0, n): # Sample number k: sample_k = np.random.choice( sample_space, sample_size, replace, prob) # Accumulate number of events: n_boys = np.count_nonzero(sample_k == 'Boy') if (n_boys in np.array([0, 1, 2])): n_event += 1 # %% Prob = relative occurencies: prob_event = n_event/n print('Probability of event =', f'{prob_event:.3f}')