draw a random number from a normal distribution numpy in python

To draw a random number from a normal distribution, we use numpy.random.normal(). This function takes three parameters, loc (mean), scale (standard deviation), and size (output shape).

Here is an example code snippet that generates a single random number from a normal distribution with mean=0 and standard deviation=1.

main.py
import numpy as np

x = np.random.normal(loc=0, scale=1, size=1)
print(x)
74 chars
5 lines

This will output a single random number from a standard normal distribution.

If you want to generate multiple numbers, simply change the size parameter. For example, to generate 10 random numbers from the same distribution, use:

main.py
x = np.random.normal(loc=0, scale=1, size=10)
print(x)
55 chars
3 lines

This will output an array of ten random numbers.

related categories

gistlibby LogSnag