← Back to NumPy Course | Chapter 9: Random Module | Lesson 6 of 6

np.random.normal/uniform

normal draws bell-curve numbers with any mean and spread; uniform draws evenly spread numbers from any range.

In this page:

  1. np.random.normal/uniform
Syntax
python
np.random.normal(loc, scale, size)
np.random.uniform(low, high, size)

np.random.normal/uniform

np.random.normal(loc, scale, size) draws from a normal distribution with mean loc and standard deviation scale. np.random.uniform(low, high, size) draws floats evenly between two values. These are the most flexible everyday distributions.

Note: With a large sample, the mean of normal values lands close to loc.

Example: np.random.normal/uniform

python
import numpy as np

np.random.seed(10)
heights = np.random.normal(170, 8, size=1000)
print(round(heights.mean(), 1), round(heights.std(), 1))
u = np.random.uniform(5, 10, size=5)
print(u.round(2))

# Output:
# 169.9 7.5
# [7.37 8.9  9.25 7.29 8.28]
Related Topics
Common Mistakes
  1. Passing variance as scale instead of standard deviation
  2. Mixing up low and high in uniform
  3. Small samples not matching expected values
Chapter Summary
  • normal takes mean and std
  • uniform takes low and high
  • scale is standard deviation
  • Large samples approach the true values
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.