0
0
NumPydata~10 mins

np.random.rand() and random arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.random.rand() and random arrays
Call np.random.rand()
Generate uniform random numbers between 0 and 1
Create array with specified shape
Return random array
The function np.random.rand() generates random numbers between 0 and 1 and returns them in an array of the shape you specify.
Execution Sample
NumPy
import numpy as np
arr = np.random.rand(3,2)
print(arr)
This code creates a 3x2 array filled with random numbers between 0 and 1.
Execution Table
StepActionShape ArgumentRandom Numbers GeneratedArray Created
1Call np.random.rand()(3, 2)6 numbers between 0 and 1Array of shape (3, 2) with random values
2Return array(3, 2)Same 6 numbersArray ready for use
3Print array(3, 2)Same 6 numbersShows array values on screen
4End---
💡 Function completes after generating and returning the random array
Variable Tracker
VariableStartAfter np.random.rand(3,2)After printFinal
arrundefined3x2 array with random floatssame array printed3x2 array with random floats
Key Moments - 3 Insights
Why do the numbers in the array change every time I run np.random.rand()?
np.random.rand() generates new random numbers each time it is called, so the array values differ on every run as shown in the execution_table rows 1 and 2.
What does the shape argument (3, 2) mean in np.random.rand(3, 2)?
The shape argument tells numpy how many random numbers to generate and how to arrange them. Here, it creates 3 rows and 2 columns, making a 3x2 array as seen in execution_table row 1.
Are the random numbers generated between 0 and 1 or some other range?
np.random.rand() always generates numbers uniformly between 0 (inclusive) and 1 (exclusive), as shown in the concept_flow step 'Generate uniform random numbers between 0 and 1'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many random numbers does np.random.rand(3, 2) generate?
A5
B6
C3
D2
💡 Hint
Check the 'Random Numbers Generated' column in step 1 of the execution_table.
At which step in the execution table is the array printed?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the action 'Print array' in the execution_table.
If you change np.random.rand(3, 2) to np.random.rand(2, 3), what changes in the execution table?
AThe number of random numbers generated changes to 5
BThe random numbers generated become integers
CThe shape argument changes to (2, 3) and array shape changes accordingly
DThe array is no longer random
💡 Hint
Refer to the 'Shape Argument' and 'Array Created' columns in the execution_table.
Concept Snapshot
np.random.rand(shape) generates random floats between 0 and 1.
The shape argument defines the array dimensions.
Returns an array filled with random numbers.
Values change every call.
Useful for simulations and testing.
Full Transcript
np.random.rand() is a function from numpy that creates arrays filled with random numbers between 0 and 1. You give it the shape you want, like (3, 2), and it returns an array with that many random numbers arranged in rows and columns. Each time you run it, the numbers change because they are randomly generated. This is useful when you want to test or simulate data. The code example shows how to create and print a 3 by 2 array of random numbers. The execution table traces the steps from calling the function, generating numbers, creating the array, printing it, and finishing. The variable tracker shows how the array variable changes from undefined to holding the random numbers. Common confusions include why numbers change every time, what the shape means, and the range of numbers generated. The quiz questions help check understanding by asking about the number of random numbers, when the array is printed, and what happens if the shape changes.