It creates an array of random numbers between 0 and 1, with the shape you specify.
Click to reveal answer
beginner
How do you create a 2x3 array of random numbers using np.random.rand()?
Use np.random.rand(2, 3). This makes 2 rows and 3 columns of random numbers between 0 and 1.
Click to reveal answer
beginner
What range of values does np.random.rand() generate?
It generates numbers from 0 (inclusive) up to 1 (exclusive). So, numbers are always >= 0 and < 1.
Click to reveal answer
intermediate
How is np.random.rand() different from np.random.randn()?
np.random.rand() generates uniform random numbers between 0 and 1, while np.random.randn() generates numbers from a normal (bell curve) distribution centered at 0.
Click to reveal answer
beginner
Why might you use np.random.rand() in data science?
To create random samples or initialize arrays for simulations, testing algorithms, or creating random data for experiments.
Click to reveal answer
What does np.random.rand(4, 2) produce?
AA 4x2 array of random numbers between 0 and 1
BA 2x4 array of random integers
CA 4x2 array of zeros
DA 4x2 array of random numbers from a normal distribution
✗ Incorrect
np.random.rand(4, 2) creates a 4 rows by 2 columns array with random floats between 0 and 1.
Which of these is true about numbers from np.random.rand()?
AThey follow a normal distribution
BThey are always between 0 and 1, including 1
CThey can be negative
DThey are always between 0 and 1, excluding 1
✗ Incorrect
Numbers from np.random.rand() are >= 0 and < 1, so 1 is excluded.
How to create a 1D array with 5 random numbers using np.random.rand()?
A<code>np.random.rand(1,5)</code>
B<code>np.random.rand(5)</code>
C<code>np.random.rand(5,1)</code>
D<code>np.random.rand()</code>
✗ Incorrect
np.random.rand(5) creates a 1D array with 5 random numbers.
What is the difference between np.random.rand() and np.random.randint()?
np.random.rand() generates floats between 0 and 1, while np.random.randint() generates random integers.
If you want a 3D array of random numbers with shape (2, 3, 4), how do you call np.random.rand()?
A<code>np.random.rand(2, 3, 4)</code>
B<code>np.random.rand(3, 4, 2)</code>
C<code>np.random.rand(2*3*4)</code>
D<code>np.random.rand([2, 3, 4])</code>
✗ Incorrect
You pass each dimension as a separate argument: np.random.rand(2, 3, 4).
Explain how to create a 2D array of random numbers between 0 and 1 using np.random.rand(). Include how to specify the shape.
Think about rows and columns as arguments.
You got /4 concepts.
Describe the difference between np.random.rand() and np.random.randn() and when you might use each.
Consider the shape of the number distribution.
You got /4 concepts.
Practice
(1/5)
1. What does the function np.random.rand(3, 2) do in NumPy?
easy
A. Creates a 3x2 array with random floats between 0 and 1
B. Creates a 3x2 array filled with zeros
C. Creates a 3x2 array with random integers
D. Creates a 3x2 array filled with ones
Solution
Step 1: Understand np.random.rand() function
This function generates random floats between 0 and 1.
Step 2: Analyze the shape arguments (3, 2)
The arguments specify the shape of the output array as 3 rows and 2 columns.
Final Answer:
Creates a 3x2 array with random floats between 0 and 1 -> Option A
Quick Check:
np.random.rand(3, 2) = 3x2 random floats [OK]
Hint: Remember rand() outputs floats in [0,1) with given shape [OK]
Common Mistakes:
Thinking it creates integers instead of floats
Confusing shape arguments with array values
Assuming it creates zeros or ones
2. Which of the following is the correct syntax to create a 1D array of 5 random floats using np.random.rand()?
easy
A. np.random.rand(5)
B. np.random.rand[5]
C. np.random.rand((5))
D. np.random.rand{5}
Solution
Step 1: Recall correct function call syntax
Functions use parentheses () to pass arguments, not brackets [] or braces {}.
Step 2: Check argument for array size
Passing 5 inside parentheses creates a 1D array of length 5.
Final Answer:
np.random.rand(5) -> Option A
Quick Check:
Correct function call uses parentheses ( ) [OK]
Hint: Use parentheses () for function calls, not brackets or braces [OK]
Common Mistakes:
Using square brackets [] instead of parentheses
Using curly braces {} which are invalid syntax
Adding extra parentheses unnecessarily
3. What is the shape and type of the output from this code?
import numpy as np
arr = np.random.rand(2, 4)
print(arr.shape, type(arr))
medium
A. (4, 2) <class 'numpy.ndarray'>
B. (2, 4) <class 'list'>
C. (2, 4) <class 'numpy.ndarray'>
D. (4, 2) <class 'list'>
Solution
Step 1: Understand np.random.rand(2, 4) output
This creates a NumPy array with shape (2, 4) filled with random floats.
Step 2: Check the type of the variable arr
It is a NumPy ndarray, not a Python list.
Final Answer:
(2, 4) <class 'numpy.ndarray'> -> Option C
Quick Check:
Shape and type match NumPy array output [OK]
Hint: np.random.rand dims = shape; output is always ndarray [OK]
Common Mistakes:
Confusing shape order (2,4) vs (4,2)
Thinking output is a Python list
Misreading print output format
4. Identify the error in this code snippet:
import numpy as np
arr = np.random.rand[3, 3]
print(arr)
medium
A. Shape arguments should be strings
B. Missing import statement for numpy
C. Incorrect print syntax
D. Using square brackets [] instead of parentheses () for function call
Solution
Step 1: Check function call syntax
np.random.rand is a function and must be called with parentheses (), not square brackets [].
Step 2: Verify other parts of code
Import is correct, print syntax is valid, shape arguments are integers as required.
Final Answer:
Using square brackets [] instead of parentheses () for function call -> Option D
Quick Check:
Function calls need parentheses ( ) [OK]
Hint: Always use () to call functions, not [] [OK]
Common Mistakes:
Using [] instead of () for function calls
Assuming shape args must be strings
Thinking print syntax is wrong
5. You want to create a 2D array with 3 rows and 4 columns filled with random floats between 0 and 1, then multiply all values by 10 to scale them. Which code correctly does this?
hard
A. arr = np.random.rand(12) * 10
B. arr = np.random.rand(3, 4) * 10
C. arr = np.random.rand(3) * 4 * 10
D. arr = np.random.rand(3, 4) + 10
Solution
Step 1: Create a 3x4 array of random floats
Use np.random.rand(3, 4) to get a 2D array with 3 rows and 4 columns.
Step 2: Scale all values by 10
Multiply the entire array by 10 to scale values from [0,1) to [0,10).
Final Answer:
arr = np.random.rand(3, 4) * 10 -> Option B
Quick Check:
Shape and scaling done correctly [OK]
Hint: Multiply after rand() to scale values, keep shape in arguments [OK]
Common Mistakes:
Adding 10 instead of multiplying
Using wrong shape arguments
Creating 1D array instead of 2D
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.