0
0
NumPydata~10 mins

Generating random samples in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generating random samples
Start
Choose distribution type
Set parameters (mean, std, etc.)
Call numpy random function
Generate samples
Use samples for analysis or visualization
End
This flow shows how to generate random samples by choosing a distribution, setting parameters, calling numpy's random functions, and then using the samples.
Execution Sample
NumPy
import numpy as np
samples = np.random.normal(loc=0, scale=1, size=5)
print(samples)
This code generates 5 random samples from a normal distribution with mean 0 and standard deviation 1.
Execution Table
StepActionFunction CallParametersResult
1Import numpyimport numpy as np-numpy module ready
2Call np.random.normalnp.random.normalloc=0, scale=1, size=5[0.5, -1.2, 0.3, 1.1, -0.7] (example)
3Print samplesprint(samples)-[0.5, -1.2, 0.3, 1.1, -0.7] (example output)
4End--Samples generated and displayed
💡 Samples generated after calling np.random.normal with specified parameters
Variable Tracker
VariableStartAfter Step 2Final
samplesundefined[0.5, -1.2, 0.3, 1.1, -0.7] (example)[0.5, -1.2, 0.3, 1.1, -0.7] (example)
Key Moments - 2 Insights
Why do the random samples change every time I run the code?
Because np.random.normal generates new random numbers each time unless you set a fixed seed. See execution_table step 2 where samples are created fresh.
What do the parameters loc, scale, and size mean?
loc is the mean (center) of the distribution, scale is the standard deviation (spread), and size is how many samples to generate. This is shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'samples' after step 2?
AUndefined
BAn array of 5 random numbers from normal distribution
CA single number
DAn empty list
💡 Hint
Check the 'Result' column in row for step 2 in execution_table
At which step are the random samples printed to the screen?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the 'Print samples' action in execution_table
If you want 10 samples instead of 5, which parameter changes?
Aloc
Bscale
Csize
Dnp.random.normal
💡 Hint
See parameters column in execution_table step 2
Concept Snapshot
Generating random samples with numpy:
Use np.random functions like np.random.normal(loc, scale, size)
loc = mean, scale = std deviation, size = number of samples
Each run produces new random values unless seed is set
Samples can be used for analysis or plotting
Full Transcript
This lesson shows how to generate random samples using numpy's random functions. First, you import numpy. Then you call a function like np.random.normal with parameters for mean (loc), standard deviation (scale), and how many samples you want (size). The function returns an array of random numbers from that distribution. Each time you run the code, you get different numbers unless you fix the random seed. Finally, you can print or use these samples for data analysis or visualization.