Bird
Raised Fist0
NumPydata~10 mins

Uniform random with random() in NumPy - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to generate a single random number between 0 and 1 using numpy.

NumPy
import numpy as np
random_number = np.random.[1]()
Drag options to blanks, or click blank then click option'
Arandom
Brandint
Cchoice
Drandn
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which returns integers.
Using randn which returns numbers from a normal distribution.
2fill in blank
medium

Complete the code to generate an array of 5 random floats between 0 and 1.

NumPy
import numpy as np
random_array = np.random.[1](5)
Drag options to blanks, or click blank then click option'
Arandom
Bchoice
Crandint
Drandn
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which returns integers.
Using randn which returns normally distributed numbers.
3fill in blank
hard

Fix the error in the code to generate a 2x3 array of random floats between 0 and 1.

NumPy
import numpy as np
random_matrix = np.random.random([1])
Drag options to blanks, or click blank then click option'
A[2,3]
B(2,3)
C{2,3}
D2,3
Attempts:
3 left
💡 Hint
Common Mistakes
Passing shape as separate arguments instead of a tuple.
Using square brackets instead of parentheses.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their random scores between 0 and 1 as values.

NumPy
import numpy as np
words = ['apple', 'banana', 'cherry']
scores = {word: np.random.[1]() for word in [2]
Drag options to blanks, or click blank then click option'
Arandom
Brandint
Cwords
Drange(3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which generates integers.
Iterating over range(3) instead of the words list.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and random scores greater than 0.5 as values.

NumPy
import numpy as np
words = ['dog', 'cat', 'bird']
result = {word.[1](): np.random.random() for word in words if np.random.random() [2] 0.5 and word [3] 'cat'}
Drag options to blanks, or click blank then click option'
Aupper
B>
C!=
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lower' instead of 'upper'.
Using '==' instead of '!=' to exclude 'cat'.

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)