Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does numpy.random.random() generate by default?

easy
A. A single random number between 0 and 1
B. A random integer between 0 and 1
C. A random number between -1 and 1
D. A list of random numbers

Solution

  1. Step 1: Understand the function purpose

    numpy.random.random() generates random floats in the range [0.0, 1.0).
  2. Step 2: Check default behavior

    Without any size argument, it returns a single float number between 0 and 1.
  3. Final Answer:

    A single random number between 0 and 1 -> Option A
  4. Quick Check:

    random() = single float [OK]
Hint: No size means one float between 0 and 1 [OK]
Common Mistakes:
  • Thinking it returns integers
  • Assuming range is -1 to 1
  • Expecting an array without size
2.

Which of the following is the correct syntax to generate a 2x3 array of uniform random numbers using numpy.random.random()?

easy
A. numpy.random.random((2,3))
B. numpy.random.random[2,3]
C. numpy.random.random{2,3}
D. numpy.random.random(2,3)

Solution

  1. Step 1: Recall correct function call

    The size parameter expects a tuple for shape, so use parentheses and commas inside.
  2. Step 2: Check syntax options

    Only numpy.random.random((2,3)) correctly passes a tuple for size.
  3. Final Answer:

    numpy.random.random((2,3)) -> Option A
  4. Quick Check:

    Tuple size = (2,3) [OK]
Hint: Use double parentheses for size tuple [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Passing size as separate arguments
  • Using curly braces instead of parentheses
3.

What is the output shape of the following code?

import numpy as np
arr = np.random.random(5)
medium
A. (1,5)
B. (5,1)
C. (5,)
D. 5

Solution

  1. Step 1: Understand the size parameter

    Passing 5 as size creates a 1D array with 5 elements.
  2. Step 2: Determine shape of the array

    A 1D array with 5 elements has shape (5,), not (1,5) or (5,1).
  3. Final Answer:

    (5,) -> Option C
  4. Quick Check:

    random(5) shape = (5,) [OK]
Hint: Single integer size makes 1D array shape (n,) [OK]
Common Mistakes:
  • Confusing 1D shape with 2D shapes
  • Expecting a scalar output
  • Assuming shape is (1,5) or (5,1)
4.

Identify the error in this code snippet:

import numpy as np
arr = np.random.random[3,4]
medium
A. Size parameter must be a single integer
B. Missing import statement
C. random() does not accept size argument
D. Using square brackets instead of parentheses for function call

Solution

  1. Step 1: Check function call syntax

    Functions in Python require parentheses, not square brackets.
  2. Step 2: Identify the error

    Using square brackets [] causes a syntax error; correct is random((3,4)).
  3. Final Answer:

    Using square brackets instead of parentheses for function call -> Option D
  4. Quick Check:

    Function calls need parentheses [OK]
Hint: Function calls always use parentheses () [OK]
Common Mistakes:
  • Using square brackets for function calls
  • Confusing size argument type
  • Assuming random() can't take size
5.

You want to create a 3x3 matrix of uniform random numbers but only want numbers greater than 0.5. Which code correctly achieves this?

hard
A. np.random.random((3,3))[np.random.random((3,3)) > 0.5]
B. np.random.random((3,3)) + 0.5
C. np.random.random((3,3)) * 0.5
D. np.random.random((3,3)) > 0.5

Solution

  1. Step 1: Understand the goal

    Create a 3x3 matrix where all uniform random numbers are greater than 0.5.
  2. Step 2: Analyze each option

    A indexes one random array with a mask from another, yielding a 1D array with values in [0,1) including some <0.5. B returns a 3x3 boolean array. C produces values in [0,0.5]. D shifts values to [0.5,1.5), ensuring all ≥0.5.
  3. Final Answer:

    np.random.random((3,3)) + 0.5 -> Option B
  4. Quick Check:

    +0.5 shifts to [0.5, 1.5) all >0.5 [OK]
Hint: Shift range by adding 0.5 to make all >0.5 [OK]
Common Mistakes:
  • Boolean indexing with different array (A: values can be <0.5, shape changes)
  • Comparison returns booleans (B)
  • Scaling down makes values ≤0.5 (C)