Search code examples
pythonmatrixjupyter-notebooknormal-distribution

Using Python, generate 100 X 100 random matrix whose entries are sampled from the normal distribution


Generate random vectors using numpy.random. Write code in Python that produces a 100 X 100 random matrix whose entries are samples from the normal distribution.


This is what I produced and am unsure if this is correct. I have a question on normal distribution as well. Should my random numbers come from a certain pool? Such as $[0,1]$ or is it possible to get values out of this range?

This is the code if wrote in Jupyter Notebook:

import numpy as np
a = np.random.randn(100,100)
a
array([[-0.42952803, -0.55136761, -0.45544016, ..., -0.54125441,
     2.31481612,  0.93721055],
   [-0.2440975 , -0.10233273, -0.06972217, ..., -0.25760561,
     0.48431004, -0.91599734],
   [-1.39176645, -0.79784139, -0.21914249, ...,  2.38224209,
     1.57696294,  0.48747715],
   ...,
   [ 0.38458431, -1.75968742,  1.64696889, ...,  1.43273609,
    -0.74896945,  0.48588267],
   [ 1.22934075,  1.27112809, -0.40593726, ...,  0.63584471,
     0.11152366, -2.23030795],
   [ 1.5910005 ,  0.29184142, -0.01811951, ..., -0.25800051,
    -0.09681777,  0.40182752]])

Solution

  • What you did is correct but there are a few things to note.

    First, np.random.randn() is specifically for drawing from the standard normal distribution (the one with mean 0 and standard deviation 1). It sounds like that is what you want based on your question but just note that you could also use np.random.normal(mu, sd, size=(100, 100)) where mu is the mean of the normal distribution you want to sample from and sd is the standard deviation.

    Regarding your question about what range values should be in, they most certainly are not constrained to [0, 1]. The normal distribution is a continuous probability density function defined on all the real numbers, so in theory you could see any real number, though the probability of observing each value decreases as you move away from the mean.

    For more about normal distributions in general, I recommend reading this page from Wolfram.