Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the random seed to 42 using numpy.
NumPy
import numpy as np np.random.[1](42)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.rand(42) instead of np.random.seed(42).
Forgetting to import numpy as np.
✗ Incorrect
The function np.random.seed() sets the random seed for reproducibility.
2fill in blank
mediumComplete the code to generate a random integer between 0 and 9 after setting the seed.
NumPy
import numpy as np np.random.seed(123) num = np.random.[1](10) print(num)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.rand(10) which generates floats, not integers.
Using np.random.random(10) which is incorrect syntax.
✗ Incorrect
np.random.randint(10) generates a random integer from 0 to 9.
3fill in blank
hardFix the error in the code to set the seed correctly before generating random numbers.
NumPy
import numpy as np np.random.[1](7) print(np.random.rand())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a value to np.random.seed instead of calling it as a function.
Using np.random.seed() without an argument.
✗ Incorrect
You must call np.random.seed(7) as a function, not assign a value to np.random.seed.
4fill in blank
hardFill both blanks to create a reproducible array of 5 random floats between 0 and 1.
NumPy
import numpy as np np.random.[1](0) arr = np.random.[2](5) print(arr)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.randint(5) which generates integers, not floats.
Not setting the seed before generating random numbers.
✗ Incorrect
np.random.seed(0) sets the seed, np.random.rand(5) generates 5 random floats.
5fill in blank
hardFill all three blanks to set the seed, generate 3 random integers from 0 to 4, and print them.
NumPy
import numpy as np np.random.[1](10) numbers = np.random.[2](0, 5, [3]) print(numbers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.rand instead of randint for integers.
Forgetting to specify the size parameter.
✗ Incorrect
np.random.seed(10) sets the seed, np.random.randint(0, 5, 3) generates 3 integers from 0 to 4.