Technology4 min read

Creating Random Valued Arrays in NumPy

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

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 keyword random because all these random functions are a part of the random module.

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 shape mentioned explicitly, filled with random integer values. If 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 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 datatype 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 shape 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 shape mentioned explicitly, filled with random values. There is a difference between randn() and rand(), the array created using rand() funciton is filled with random samples from a uniform distribution over [0, 1) whereas the array created using the randn() function is filled with random values from 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.

You may also like:


Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next