np.random.rand/randn
rand gives uniform numbers between 0 and 1 and randn gives bell-curve numbers centered on 0.
In this page:
Syntax
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
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
- Passing a tuple to rand or randn
- Confusing uniform and normal
- 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: