We use np.random.rand() to create arrays filled with random numbers between 0 and 1. This helps us test ideas or simulate real-world data when we don't have actual data.
np.random.rand() and random arrays in NumPy
import numpy as np # Create a random array with shape (d1, d2, ..., dn) random_array = np.random.rand(d1, d2, ..., dn)
The arguments d1, d2, ..., dn are the dimensions of the array you want.
The numbers generated are floats between 0 (inclusive) and 1 (exclusive).
import numpy as np # 1D array with 5 random numbers array_1d = np.random.rand(5) print(array_1d)
import numpy as np # 2D array with 3 rows and 4 columns array_2d = np.random.rand(3, 4) print(array_2d)
import numpy as np # Edge case: zero size array empty_array = np.random.rand(0) print(empty_array)
import numpy as np # 3D array with shape (2, 1, 3) array_3d = np.random.rand(2, 1, 3) print(array_3d)
This program creates two different 2D random arrays with 2 rows and 3 columns each. It prints both arrays so you can see the random numbers generated.
import numpy as np # Create a 2D random array with 2 rows and 3 columns random_array_before = np.random.rand(2, 3) print("Random array before:") print(random_array_before) # Create another random array with the same shape random_array_after = np.random.rand(2, 3) print("\nRandom array after:") print(random_array_after)
Time complexity: Generating random numbers with np.random.rand() is very fast and runs in O(n) where n is the total number of elements.
Space complexity: The space used is proportional to the size of the array you create.
Common mistake: Forgetting that the numbers are always between 0 and 1, so if you want other ranges, you need to scale or shift the numbers.
When to use: Use np.random.rand() when you want random floats between 0 and 1. For other distributions or integer random numbers, use other functions like np.random.randint() or np.random.normal().
np.random.rand() creates arrays filled with random floats between 0 and 1.
You specify the shape by giving dimensions as arguments.
It is useful for testing, simulations, and initializing random data.
Practice
np.random.rand(3, 2) do in NumPy?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 AQuick Check:
np.random.rand(3, 2) = 3x2 random floats [OK]
- Thinking it creates integers instead of floats
- Confusing shape arguments with array values
- Assuming it creates zeros or ones
np.random.rand()?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 AQuick Check:
Correct function call uses parentheses ( ) [OK]
- Using square brackets [] instead of parentheses
- Using curly braces {} which are invalid syntax
- Adding extra parentheses unnecessarily
import numpy as np arr = np.random.rand(2, 4) print(arr.shape, type(arr))
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 CQuick Check:
Shape and type match NumPy array output [OK]
- Confusing shape order (2,4) vs (4,2)
- Thinking output is a Python list
- Misreading print output format
import numpy as np arr = np.random.rand[3, 3] print(arr)
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 DQuick Check:
Function calls need parentheses ( ) [OK]
- Using [] instead of () for function calls
- Assuming shape args must be strings
- Thinking print syntax is wrong
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 BQuick Check:
Shape and scaling done correctly [OK]
- Adding 10 instead of multiplying
- Using wrong shape arguments
- Creating 1D array instead of 2D
Ready to prove it?
Pass the test and this pattern moves unknown
