0
0
NumPydata~20 mins

Uniform random with random() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Uniform Random Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy.random.random() shape
What is the shape of the array produced by numpy.random.random((3, 4))?
NumPy
import numpy as np
arr = np.random.random((3, 4))
print(arr.shape)
A(12,)
B(4, 3)
C(3, 4)
D(3,)
Attempts:
2 left
💡 Hint
The argument to random() defines the shape of the output array.
data_output
intermediate
2:00remaining
Range of values from numpy.random.random()
What is the range of values produced by numpy.random.random(5)?
NumPy
import numpy as np
arr = np.random.random(5)
print(arr)
AValues between 0 and 1, including 0 but excluding 1
BValues between 0 and 1, including both 0 and 1
CValues between -1 and 1
DValues between 1 and 10
Attempts:
2 left
💡 Hint
random() generates floats in the half-open interval [0.0, 1.0).
visualization
advanced
3:00remaining
Histogram of uniform random numbers
Which option shows the correct histogram plot code for 1000 samples from numpy.random.random()?
NumPy
import numpy as np
import matplotlib.pyplot as plt
samples = np.random.random(1000)
Aplt.plot(samples); plt.show()
Bplt.hist(samples, bins=10, range=(0,1)); plt.show()
Cplt.scatter(range(1000), samples); plt.show()
Dplt.bar(range(10), samples[:10]); plt.show()
Attempts:
2 left
💡 Hint
Histograms show frequency distribution of values.
🧠 Conceptual
advanced
2:00remaining
Effect of seed on numpy.random.random()
What happens if you set the seed using np.random.seed(42) before calling np.random.random(3) twice?
AThe second call produces an error
BBoth calls produce different arrays of 3 random numbers
CThe seed only affects integers, not floats
DBoth calls produce the same array of 3 random numbers
Attempts:
2 left
💡 Hint
Seed fixes the random number generator state.
🔧 Debug
expert
2:00remaining
Identify error in numpy.random.random() usage
What error does this code raise?
import numpy as np
arr = np.random.random(3,4)
print(arr)
ATypeError: random() takes from 0 to 1 positional arguments but 2 were given
BValueError: shape must be a tuple of integers
CSyntaxError: invalid syntax
DNo error, prints a 3x4 array
Attempts:
2 left
💡 Hint
random() expects a single argument for shape, which should be a tuple.