Numpy random Examples | rand() | randint() function

 

Introduction to Python Numpy random numbers

Random numbers are the numbers that return a random integer. The random number does not mean a different number every time, but it means something that cannot be predicted logically. We might get the same number two times while creating a random number. In Python, there are different libraries that can help us to create random numbers. For example, by using a random module or Numpy module, we can create different kinds of random numbers. In this tutorial, we will learn about creating random numbers in the NumPy module.

 

Getting started with Numpy random numbers in Python

A random number is a number generated using a large set of numbers and a mathematical algorithm that gives equal probability to all the numbers occurring in the specified distribution. Random numbers are most commonly produced with the help of random number generators. They have their important applications especially in cryptography where they act as ingredients in the encryption keys.  In Python, there are some modules that help us to generate random numbers, and NumPy is one of them.

Advertisement

NumPy does not come with python when we installed it. We have to manually install NumPy on our environment in order to use it in our program. We can install NumPy by using the pip command. See the example below:

pip install numpy

Once we successfully installed the NumPy module, we will be then able to use it. In order to check if the NumPy module had been installed successfully or not, just import it and run the code. If the program runs without any error that means we had successfully installed the NumPy module.  See the code below:

import numpy as np

Once we know that the NumPy is successfully installed, we are good to access its various functionalities.

 

Example-1: Use random.randint() to generate random integers

The NumPy module comes with a lot of important and powerful methods. Once we successfully import the NumPy module, we will be able to use powerful methods. One of those methods is randint() which is used to generate a random number. Let us now import the NumPy module and use its method randint() to generate a random number. The syntax looks like this:

random.randint(range_of_number)

Now let us generate random numbers using NumPy. See the code below:

# importing random form numpy module
from numpy import random

# generating numpy random number
random_number1 = random.randint(100)
random_number2 = random.randint(100)
random_number3 = random.randint(100)

# printing the number
print(random_number1)
print(random_number2)
print(random_number3)

Output:

Advertisement
40
52
85

Every time we run the program, we will get the different numbers and might also get the same number as well. The randint() method also allows us to specify the starting range. For example, if we want to generate a random number between 50 and 100, we can do that. See the example below:

# importing random form numpy module
from numpy import random

# generating random number
random_number1 = random.randint(50, 100)
random_number2 = random.randint(50, 100)
random_number3 = random.randint(50, 100)

# printing the number
print(random_number1)
print(random_number2)
print(random_number3)

Output:

97
88
58

If you run the same code on your PC, you most likely will get different output, and it is totally fine as far as you are working with random numbers.

 

Example-2: Use random.randint() to generate random array

The randint() methods take an optional parameter named size, which is used to specify the number of random numbers to be generated. The generated random number will be returned in the form of a NumPy array. The simple syntax of creating an array of random numbers in NumPy looks like this:

random.randint(number range, size = (number_of_elements))

See the example below which generates random numbers in the form of a NumPy array.

# importing random form numpy module
from numpy import random

# generating random number
random_number1 = random.randint(100, size=(3))

# printing the number
print(random_number1)

Output:

[58 51 89]

In a similar way, we can create a two-dimensional array as well. We just need to specify the parameter size. See the following example, which creates two-directional a NumPy array.

# importing random form numpy module
from numpy import random

# generating random number
random_number1 = random.randint(100, size=(3, 2))

# printing the number
print(random_number1)

Output:

Advertisement
[[63 87]
[44 83]
[68 54]]

 

Example-3: Use random.rand() to generate random float numbers

In the above example, notice that the random number that we generate using the NumPy module was an integer. In this section, we will see how we can generate a float number. The syntax for generating random float numbers in NumPy is given below:

random.rand()

Now let us create a float random number. See the example below:

# importing random form numpy module
from numpy import random

# generating random float number
random_number1 = random.rand()
random_number2 = random.rand()
random_number3 = random.rand()

# printing the number
print(random_number1)
print(random_number2)
print(random_number3)

Output:

0.686349836407897
0.856053195047545
0.28787495686164977

Notice that all the outputs are less than 1 and greater than zero. It is because .rand() function generates a random float number between 0 and 1. It takes an optional parameter which is to specify the number of random float numbers to be generated.  See the example below, where we created three different float random numbers.

# importing random form numpy module
from numpy import random

# generating random number
random_number1 = random.rand(3)
random_number2 = random.rand(3)
random_number3 = random.rand(3)

# printing the number
print(random_number1)
print(random_number2)
print(random_number3)

Output:

[0.8602739     0.02930119     0.08135595]
[0.76216235    0.64040494     0.48904807]
[0.62767582    0.41621455      0.5803977 ]

Notice that the rand() method returned a NumPy array of random float numbers. In a similar way, we can create two dimensional NumPy array of random float numbers as well. See the example below:

# importing random form numpy module
from numpy import random

# generating random number
random_number1 = random.rand(3, 2)

# printing the number
print(random_number1)

Output:

[[0.04358036   0.60738349]
[0.41667409   0.08495522]
[0.25213312   0.5538406 ]]

 

Example-4: Use random.choice() to generate random numbers

So far we have learned how to generate random numbers in NumPy using its built-in functions. Now let us see how we can generate a random number from an array containing different numbers. The simple syntax for the generating random number from an array looks like this:

Advertisement
random.choice(numpy_array)

Now let us take a practical example and see how we can generate a random number from a given array. See the program below:

# importing random from numpy
from numpy import random
import  numpy as np

# creating numpy array
array = np.array([1, 2, 3, 4, 5])

# creating random number from array
random1 = random.choice(array)
random2 = random.choice(array)

# printing
print(random1)
print(random2)

Output:

5
2

We can also use the same method randon.choice() to create a NumPy array of random numbers as well. See the following example where we created a NumPy array of random numbers between 0 and 10.

# importing random from numpy
from numpy import random

# creating array
array = random.choice(10, 2,)

# printing the array
print(array)

Output:

[6 5]

 

Example-5: Use random.randn() to generate random numbers

The random.randn() function creates an array of specified shape and fills it with random values as per standard normal distribution. If positive arguments are provided, randn() generates an array of shape (d0, d1, …, dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1. A single float randomly sampled from the distribution is returned if no argument is provided. The simple syntax looks like this:

random.rand()

Now let us see an example with and without arguments. See the program below:

# importing random from numpy
from numpy import random

# creates random number
random_num = random.randn()

# printing
print(random_num)

Output:

1.4602804207013793

Now let us pass arguments and generated a NumPy array with randomly floating numbers. See the example below:

# importing random from numpy
from numpy import random

# creates random number
random_num = random.randn(4)

# printing
print(random_num)

Output:

[-0.85547493 0.5898579 -0.54645818 -1.51666755]

Notice that it created a NumPy array of random values, even with negative values as well. In similar way we can also create two dimensional NumPy array of random values using .randn() method. See the example below:

# importing random from numpy
from numpy import random

# creates random number
random_num = random.randn(4, 2)

# printing
print(random_num)

Output:

[[ 0.12581119 0.25076374]
[-0.42722043 -0.53812873]
[ 1.11294732 0.94741872]
[ 0.21079132 -1.22844037]]

 

Example-6: Use random.uniform() to generate uniform random numbers

So far we have many functions in NumPy that we can use to generate random numbers. However, all the numbers that we generate were not uniformly distributed. In NumPy, we have this feature of creating random numbers from a uniform distribution. We can .unifrom() method to create random numbers. The simple syntax looks like this.

random.uniform()

With the help of random.uniform() method, we can get the random samples from uniform distribution and returns the random samples as NumPy array by using this method. Now let us take an example and see how this method works.  See the python program below:

# importing random from numpy
from numpy import random

# using unifrom
random_num = random.uniform(0,5,10)

# printing
print(random_num)

Output:

[3.41493285 4.1154541 3.65265425 3.25288431 4.31390089 0.29230041
4.90267289 4.22634007 1.77893022 3.76995904]

 

Summary

A random number is a number generated using a large set of numbers and a mathematical algorithm that gives equal probability to all numbers occurring in the specified distribution. Random numbers are most commonly produced with the help of a random number generator. Random numbers have important applications, especially in cryptography where they act as ingredients in encryption keys. In this tutorial, we learned about how to create random numbers in the NumPy module. The random method in NumPy helps us to create random numbers. We covered different ways to create randoms numbers along with various examples. Moreover, we also discussed how we create a random valued NumPy array and also how to generate random values from an Array. All in all, this tutorial covers all the methods and various examples to make you understand random numbers in NumPy.

 

Further Reading

random numbers in numpy
Numpy and random numbers
rand method in numpy

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment