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

np.random.rand/randn

rand gives uniform numbers between 0 and 1 and randn gives bell-curve numbers centered on 0.

In this page:

  1. np.random.rand/randn
Syntax
python
np.random.rand(d0, d1)
np.random.randn(d0, d1)

np.random.rand/randn

np.random.rand(d0, d1, ...) draws uniform values in [0, 1). np.random.randn draws from the standard normal distribution with mean 0 and standard deviation 1. Both take dimensions as separate arguments, not a tuple.

Note: To get mean m and standard deviation s from randn, use m + s * randn(...).

Example: np.random.rand/randn

python
import numpy as np

np.random.seed(1)
print(np.random.rand(2, 3).round(3))
print(np.random.randn(3).round(3))

# Output:
# [[0.417 0.72  0.   ]
#  [0.302 0.147 0.092]]
# [-0.528 -1.073  0.865]
Related Topics
Common Mistakes
  1. Passing a tuple to rand or randn
  2. Confusing uniform and normal
  3. Forgetting values are floats
Chapter Summary
  • rand is uniform on 0 to 1
  • randn is standard normal
  • Dimensions are separate arguments
  • Scale with mean + std * randn
🔒

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.