Bird
Raised Fist0
NumPydata~20 mins

Normal distribution with normal() in NumPy - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Normal Distribution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of numpy normal() with fixed seed
What is the output of this code snippet that generates 3 random numbers from a normal distribution with mean 0 and standard deviation 1?
NumPy
import numpy as np
np.random.seed(0)
samples = np.random.normal(0, 1, 3)
print(samples)
A[0.49671415 -0.1382643 0.64768854]
B[1.76405235 0.40015721 0.97873798]
C[ 0.76103773 0.12167502 0.44386323]
D[ 1.86755799 -0.97727788 0.95008842]
Attempts:
2 left
💡 Hint
Remember to set the random seed before generating samples to get reproducible results.
❓ data_output
intermediate
1:30remaining
Shape of output array from normal()
What is the shape of the array produced by this code?
NumPy
import numpy as np
result = np.random.normal(loc=5, scale=2, size=(4,3))
print(result.shape)
A(4, 3)
B(4,)
C(12,)
D(3, 4)
Attempts:
2 left
💡 Hint
The size parameter defines the shape of the output array.
❓ visualization
advanced
2:30remaining
Histogram of samples from normal distribution
Which option shows the correct histogram plot code for 1000 samples from a normal distribution with mean 0 and standard deviation 1?
NumPy
import numpy as np
import matplotlib.pyplot as plt
samples = np.random.normal(0, 1, 1000)
# Fill in the code to plot histogram
A
plt.bar(range(30), samples[:30])
plt.title('Bar plot of first 30 samples')
plt.show()
B
plt.plot(samples)
plt.title('Line plot of samples')
plt.show()
C
plt.scatter(range(1000), samples)
plt.title('Scatter plot of samples')
plt.show()
D
plt.hist(samples, bins=30, color='blue', edgecolor='black')
plt.title('Histogram of Normal Samples')
plt.show()
Attempts:
2 left
💡 Hint
Histograms show frequency distribution of data values.
🧠 Conceptual
advanced
1:30remaining
Effect of scale parameter in normal()
What happens to the spread of data when you increase the scale parameter in np.random.normal(loc=0, scale=scale_value, size=1000)?
AThe data points become more concentrated around the mean, decreasing the standard deviation.
BThe mean of the data shifts to the scale value.
CThe data points become more spread out, increasing the standard deviation.
DThe number of data points generated increases.
Attempts:
2 left
💡 Hint
Scale is the standard deviation of the normal distribution.
🔧 Debug
expert
2:00remaining
Identify error in normal() usage
What error will this code raise and why? import numpy as np samples = np.random.normal(0, -1, 5) print(samples)
NumPy
import numpy as np
samples = np.random.normal(0, -1, 5)
print(samples)
AValueError: scale < 0
BTypeError: loc must be a float
CSyntaxError: invalid syntax
DNo error, prints 5 samples
Attempts:
2 left
💡 Hint
Scale (standard deviation) must be positive.

Practice

(1/5)
1. What does the loc parameter control in the numpy.random.normal() function?
easy
A. The spread (standard deviation) of the normal distribution
B. The center (mean) of the normal distribution
C. The number of random values generated
D. The shape of the distribution curve

Solution

  1. Step 1: Understand the parameters of normal()

    The normal() function has parameters loc and scale. loc sets the mean (center) of the distribution.
  2. Step 2: Identify the role of loc

    The mean is the center point where most values cluster in a bell curve.
  3. Final Answer:

    The center (mean) of the normal distribution -> Option B
  4. Quick Check:

    loc = center [OK]
Hint: Remember: loc = center, scale = spread [OK]
Common Mistakes:
  • Confusing loc with scale
  • Thinking loc controls number of samples
  • Assuming loc changes distribution shape
2. Which of the following is the correct syntax to generate 5 random numbers from a normal distribution with mean 10 and standard deviation 2 using numpy?
easy
A. numpy.random.normal(size=5, mean=10, std=2)
B. numpy.normal(10, 2, 5)
C. numpy.random.normal(5, loc=10, scale=2)
D. numpy.random.normal(loc=10, scale=2, size=5)

Solution

  1. Step 1: Recall the correct function and parameters

    The function is numpy.random.normal() with parameters loc for mean, scale for std dev, and size for number of samples.
  2. Step 2: Match parameters to correct syntax

    numpy.random.normal(loc=10, scale=2, size=5) correctly uses loc=10, scale=2, and size=5.
  3. Final Answer:

    numpy.random.normal(loc=10, scale=2, size=5) -> Option D
  4. Quick Check:

    Correct parameter names and order [OK]
Hint: Use loc=mean, scale=std, size=number [OK]
Common Mistakes:
  • Using wrong parameter names like mean or std
  • Mixing order without keywords
  • Calling numpy.normal instead of numpy.random.normal
3. What is the output shape of the following code?
import numpy as np
arr = np.random.normal(loc=0, scale=1, size=(3,4))
print(arr.shape)
medium
A. (12,)
B. (4, 3)
C. (3, 4)
D. (3,)

Solution

  1. Step 1: Understand the size parameter

    The size argument is set to (3,4), which means generate a 2D array with 3 rows and 4 columns.
  2. Step 2: Check the shape of the generated array

    Printing arr.shape returns the shape tuple, which matches the size argument.
  3. Final Answer:

    (3, 4) -> Option C
  4. Quick Check:

    size=(3,4) means shape=(3,4) [OK]
Hint: size tuple = output shape [OK]
Common Mistakes:
  • Confusing rows and columns order
  • Expecting flattened array shape
  • Ignoring tuple format for size
4. Identify the error in this code snippet:
import numpy as np
samples = np.random.normal(mean=0, std=1, size=10)
print(samples)
medium
A. Incorrect parameter names: should use loc and scale instead of mean and std
B. Missing import statement for numpy
C. size parameter must be a tuple, not an integer
D. The print statement syntax is wrong

Solution

  1. Step 1: Check parameter names for normal()

    The function np.random.normal() expects loc for mean and scale for standard deviation, not mean or std.
  2. Step 2: Verify other parts of the code

    Import is correct, size can be integer, and print syntax is valid.
  3. Final Answer:

    Incorrect parameter names: should use loc and scale instead of mean and std -> Option A
  4. Quick Check:

    Use loc and scale for mean and std [OK]
Hint: Use loc=mean, scale=std; mean/std are invalid [OK]
Common Mistakes:
  • Using mean or std instead of loc and scale
  • Thinking size must be tuple always
  • Assuming print syntax error
5. You want to simulate daily temperatures for a week that average 20°C with a standard deviation of 3°C. Which code correctly generates this data and calculates the average temperature?
hard
A. temps = np.random.normal(loc=20, scale=3, size=7) avg_temp = temps.mean() print(round(avg_temp, 2))
B. temps = np.random.normal(mean=20, std=3, size=7) avg_temp = temps.sum() print(avg_temp)
C. temps = np.random.normal(loc=3, scale=20, size=7) avg_temp = temps.mean() print(avg_temp)
D. temps = np.random.normal(loc=20, scale=3, size=7) avg_temp = temps.median() print(avg_temp)

Solution

  1. Step 1: Generate temperatures with correct parameters

    Use loc=20 for mean temperature and scale=3 for standard deviation, with size=7 for a week.
  2. Step 2: Calculate the average temperature correctly

    Use temps.mean() to get the average. Round for neat output.
  3. Final Answer:

    temps = np.random.normal(loc=20, scale=3, size=7) avg_temp = temps.mean() print(round(avg_temp, 2)) -> Option A
  4. Quick Check:

    loc=mean, scale=std, mean() for average [OK]
Hint: Use loc=mean, scale=std, mean() to average [OK]
Common Mistakes:
  • Swapping loc and scale values
  • Using mean or std instead of loc and scale
  • Using sum() instead of mean() for average
  • Using median() instead of mean()