Signup/Sign In

Creating Random Valued Arrays in NumPy

Posted in Programming   LAST UPDATED: MARCH 25, 2023

    In my last blog post I covered various different ways of creating a Numpy array, today we will discover random functions present in the NumPy's random module to create Numpy arrays with random values.

    We can create NumPy arrays filled with random values, these random values can be integers, normal values(based on the normal distribution) or uniform values(based on the uniform distribution).

    The important point to note is, to access any of the random functions we need to include the keyword random because all these random functions are a part of the random module.

    Creating Random Valued Arrays in NumPy

    Let's take a look at these functions one by one


    1. Using Numpy randint() function

    Using this function we can create a NumPy array filled with random integers values. This function returns an array of shapes mentioned explicitly, filled with random integer values. If the size parameter is not explicitly mentioned this function will just return a random integer value between the range mentioned instead of the array.

    Parameters :

    • Low: This is the lower limit of the range or the starting point of values we need in our array to be created. It is inclusive (is included).

    • High: This is the upper limit of the range or the ending point of values we need in our array to be created. It is exclusive (is not included).

      For example, if we want values in our array to be in the range [5,20) the lowest value in the array would start from 5 going on till 20. But 5 is inclusive and 20 is exclusive, which means the values would be confined between 5 and 19.

    • Size: This is the parameter that decides the shape of the array. We pass in the shape of the array here. For example for an array with 4 rows and 3 columns we pass (4, 3). But remember to pass the size as a tuple.

    • dtype: This parameter is used to specify the data type of the values to be stored in the array to be created.

    Let's take a simple code example and see,

    import numpy as np
    
    # if the shape is not mentioned the output will just be a random integer in the given range
    rand_int = np.random.randint(5,10)  
    print("First array", rand_int)
    
    rand_int2 = np.random.randint(10,90,(4,5)) # random numpy array of shape (4,5)
    print("Second array", rand_int2)
    
    rand_int3 = np.random.randint(50,75,(2,2), dtype='int64')
    print("Third array", rand_int3)
    

    Output:

    First array 7
    Second array [[86 10 37 84 40]
     [66 49 29 73 37]
     [39 87 36 20 87]
     [76 86 89 69 50]]
    Third array [[74 73]
     [71 50]]


    2. Using Numpy randn() function

    This function returns an array of shapes mentioned explicitly, filled with values from the standard normal distribution. The values are always floating-point numbers based on the normal distribution having the mean equal to 0 and variation equal to 1.

    Parameters:

    The randn() function only takes one parameter as input, size which is optional.

    • Size: The size here represents the dimensions of the returned array. If nothing is passed we will get a single floating-point number as output.

    Let's take a code example,

    import numpy as np
    
    rand_n = np.random.randn() # outputs a single floating point number
    print("First array", rand_n)
    
    rand_n2 = np.random.randn(4,5) # outputs an 4x5 array filled with random floating point numbers 
    print("Second array", rand_n2)

    Output:

    First array -0.16000824895754412
    Second array [[ 1.50870984 -0.30902038 -0.93408267  2.85782319  1.28046521]
     [-0.42138647 -0.0910151  -2.24334255  0.06135505 -0.11190143]
     [-0.45479495 -0.80909493 -0.46962061  0.21875305  0.45955272]
     [ 0.31418762  0.66268862 -0.27700588 -0.5103291  -0.68195657]]


    3. Using Numpy rand() function

    This function returns an array of shapes mentioned explicitly, filled with random values. There is a difference between randn() and rand(), the array created using rand() function is filled with random samples from a uniform distribution over [0, 1) whereas the array created using the randn() the function is filled with random values from a normal distribution.

    Parameters:

    The rand() function also takes one parameter as input, size which is optional.

    • Size: Similar to randn() function, size here represents the dimensions of the returned array. If nothing is passed we will get a single floating-point number as output.

    import numpy as np
    
    rand = np.random.rand() # outputs single floating-point value .
    print("First array", rand)
    
    rand2 = np.random.rand(3,2) # outputs an 3x2 array filled with random floating point values.
    print("Second array", rand2)

    Output:

    First array 0.7578858928993336
    Second array [[0.15647101 0.13870584]
     [0.89344256 0.68226333]
     [0.46247578 0.09658549]]


    Conclusion

    Generally, we use randint() function when we need random integer values but the randn() function on the other hand is used when we want floating-point random numbers that are both positive and negative. Finally, the rand() function unlike randn() is used when we want random floating-point numbers that are only positive and in the range [0,1).

    So, you just learned how to use random functions to generate random NumPy arrays. These random functions are extremely useful and widely used in machine learning and deep learning.

    Keep Learning! Stay Curious.


    Frequently Asked Questions

    1: How do I create a random valued array in NumPy?

    You can use the NumPy function "random" to create a random valued array. For example, "np.random.rand(3, 4)" will create a 3x4 array of random values.

    2: How do I set the seed for a random valued array in NumPy?

    Use the NumPy function "seed" to set the seed for the random valued array. For example, "np.random.seed(123)" will set the seed to 123.

    3: How do I create an integer random-valued array in NumPy?

    Use the NumPy function "random.randint" to create an integer random valued array. For example, "np.random.randint(low=0, high=10, size=(3, 4))" will create a 3x4 array of integers between 0 and 10.

    4: How do I create a normal distribution random valued array in NumPy?

    Use the NumPy function "random.normal" to create a normal distribution random valued array. For example, "np.random.normal(loc=0, scale=1, size=(3, 4))" will create a 3x4 array of random values with a mean of 0 and standard deviation of 1.

    You may also like:

    About the author:
    I am an undergraduate pursuing B.Tech Computer Science, currently in 3rd year.I love to manipulate data to make sense out of it, my interests include machine learning and deep learning.I love to code in python.
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS