0
0
NumPydata~20 mins

np.random.rand() and random arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output shape of np.random.rand() with multiple dimensions
What is the shape of the array produced by np.random.rand(3, 4, 2)?
NumPy
import numpy as np
arr = np.random.rand(3, 4, 2)
print(arr.shape)
A(2, 3, 4)
B(3, 2, 4)
C(4, 3, 2)
D(3, 4, 2)
Attempts:
2 left
💡 Hint
The arguments to np.random.rand() specify the size of each dimension in order.
data_output
intermediate
1:30remaining
Range of values from np.random.rand()
What is the range of values you can expect from np.random.rand(5)?
NumPy
import numpy as np
arr = np.random.rand(5)
print(arr.min(), arr.max())
AValues between 0 and 1 inclusive
BValues between 0 (inclusive) and 1 (exclusive)
CValues between -1 and 1
DValues between 0 and 5
Attempts:
2 left
💡 Hint
np.random.rand() generates floats in a specific range starting at zero.
visualization
advanced
2:00remaining
Visualizing distribution of np.random.rand() values
Which option correctly plots a histogram showing the distribution of 1000 values generated by np.random.rand(1000)?
NumPy
import numpy as np
import matplotlib.pyplot as plt
values = np.random.rand(1000)
plt.hist(values, bins=20)
plt.show()
AA histogram with a peak near 0.5
BA histogram with a peak near 0 and decreasing towards 1
CA histogram with bars roughly uniform across the range 0 to 1
DA histogram with values only at 0 and 1
Attempts:
2 left
💡 Hint
np.random.rand() generates uniform random numbers between 0 and 1.
🧠 Conceptual
advanced
1:30remaining
Effect of shape argument order in np.random.rand()
If you want to create a 2D array with 5 rows and 3 columns using np.random.rand(), which call is correct?
Anp.random.rand(5, 3)
Bnp.random.rand(3, 5)
Cnp.random.rand([5, 3])
Dnp.random.rand((5, 3))
Attempts:
2 left
💡 Hint
The arguments to np.random.rand() are separate integers for each dimension.
🔧 Debug
expert
2:00remaining
Identify the error in this np.random.rand() usage
What error will this code raise?
import numpy as np
arr = np.random.rand([2, 3])
print(arr.shape)
NumPy
import numpy as np
arr = np.random.rand([2, 3])
print(arr.shape)
ATypeError: 'list' object cannot be interpreted as an integer
BSyntaxError: invalid syntax
CValueError: shape must be a tuple of integers
DNo error, prints (2, 3)
Attempts:
2 left
💡 Hint
np.random.rand() expects separate integer arguments, not a list.