0
0
NumPydata~10 mins

Monte Carlo simulation basics in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to generate 1000 random numbers between 0 and 1 using NumPy.

NumPy
import numpy as np
random_numbers = np.random.[1](1000)
Drag options to blanks, or click blank then click option'
Arandint
Brand
Crandom_sample
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which generates integers, not floats.
Using random_sample which is an alias but not the most common.
2fill in blank
medium

Complete the code to estimate the probability of getting a value less than 0.5 from the random numbers.

NumPy
prob = np.mean(random_numbers [1] 0.5)
Drag options to blanks, or click blank then click option'
A==
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than operator which counts wrong values.
Using equality which is too strict.
3fill in blank
hard

Fix the error in the code to simulate rolling a six-sided die 1000 times.

NumPy
rolls = np.random.randint(1, [1], size=1000)
Drag options to blanks, or click blank then click option'
A5
B6
C0
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 as upper bound excludes 6 from results.
Using 0 as upper bound causes an error.
4fill in blank
hard

Fill both blanks to create a dictionary with counts of rolls greater than 4 and less than or equal to 4.

NumPy
counts = { 'gt_4': np.sum(rolls [1] 4), 'le_4': np.sum(rolls [2] 4) }
Drag options to blanks, or click blank then click option'
A>
B<=
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= for 'le_4'.
Mixing up operators causing wrong counts.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that counts how many rolls are equal to each face from 1 to 6.

NumPy
face_counts = { [1]: np.sum(rolls [2] [3]) for [1] in range(1, 7) }
Drag options to blanks, or click blank then click option'
Aface
B==
Drolls
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rolls' as key instead of 'face'.
Using assignment '=' instead of comparison '=='.