Complete the code to generate 1000 random numbers between 0 and 1 using NumPy.
import numpy as np random_numbers = np.random.[1](1000)
The np.random.rand function generates random numbers between 0 and 1.
Complete the code to estimate the probability of getting a value less than 0.5 from the random numbers.
prob = np.mean(random_numbers [1] 0.5)
We check which numbers are less than 0.5 and take the mean to estimate the probability.
Fix the error in the code to simulate rolling a six-sided die 1000 times.
rolls = np.random.randint(1, [1], size=1000)
The upper bound in randint is exclusive, so to include 6, we use 7.
Fill both blanks to create a dictionary with counts of rolls greater than 4 and less than or equal to 4.
counts = { 'gt_4': np.sum(rolls [1] 4), 'le_4': np.sum(rolls [2] 4) }We count rolls greater than 4 and rolls less than or equal to 4 using > and <= operators.
Fill all three blanks to create a dictionary comprehension that counts how many rolls are equal to each face from 1 to 6.
face_counts = { [1]: np.sum(rolls [2] [3]) for [1] in range(1, 7) }The dictionary comprehension uses face as key, checks where rolls equal face with ==, and sums those occurrences.