NumPy Random Numbers (Python Tutorial)

NumPy Random Numbers

This is a detailed tutorial of Random Numbers in NumPy. Learn the usage of Random numbers in NumPy to create values & arrays.

Random Numbers in NumPy

We use various sets of numbers in NumPy, and by the random number, we don’t mean a different number every time. In random numbers, we have a number whose prediction cannot be done logically. This number has to be really random and should be not the result of any algorithm or program.

Pseudo-Random:

As we know, there is a program to generate everything in a programming language even if it a random number. And if there is some code that can predict a random number, it means that those numbers are not completely random. So such numbers which are the outcome of a certain code or algorithm is known as pseudo-random.

True-Random

In order to have a truly random number, we need to have some random data from completely outside sources. We can have this data from some networks or keystrokes. But we do not have much use of these truly random numbers whereas pseudo-random numbers are used more.

Generating Random Numbers

In order to generate random numbers in NumPy, we have a module known as random. This offers us functionality to work with random numbers in NumPy.

Let us take an example to generate some random numbers using this module:

#importing the numpy package with random module
from numpy import random
# here we will use the random module
a=random.randint(50)
# here we will print the array 
print(a)

Output.

45

So here we are getting a random number between 0 and 50.

Let us take another example:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.randint(200) 
# here we will print the array 
print(a)

Output.

109

Here we get a random number between 0 and 200.

Generate Random Float

In order to have float numbers, we have a method in NumPy which gives us float values between 0 and 1. This method is known as rand().

Let us take an example for this method:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.rand() 
# here we will print the array 
print(a)

Output.

0.6964691855978616

Generate Random Array

As we know in NumPy, we work with multi-dimensional arrays, and we can use these above-mentioned methods in order to generate random arrays.

For integer we use randint() a method that will take a size parameter with the help of which we can specify the shape of the array.

Let us take an example:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.randint(200, size=(6)) 
# here we will print the array 
print(a)

Output.

[109 126  66  98  17  83]

Here we are getting an array with six elements with elements in-between range of 0 and 200. This is a one-dimensional array.

Let us take another example:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.randint(200, size=(4,6)) 
# here we will print the array 
print(a)

Output.

[[109 126  66  98  17  83]
 [106 123  57  96 113 126]
 [ 47  73  32 174 111 153]
 [ 83  78 164  96  68  49]]

Here we have an array with four rows and six elements in each row. And this will give us a two-dimensional array.

For Float number we use rand() a method which also allows us to specify the shape of the array.

Let us go through an example fro this:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.rand(6) 
# here we will print the array 
print(a)

Output.

[ 0.69646919  0.28613933  0.22685145  0.55131477  0.71946897  0.42310646]

Here in this example, we get an array with numbers between 0 and 1. These are float numbers in a one-dimensional array.

Let us take another for this:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.rand(4,6) 
# here we will print the array 
print(a)

Output.

[[ 0.69646919  0.28613933  0.22685145  0.55131477  0.71946897  0.42310646]
 [ 0.9807642   0.68482974  0.4809319   0.39211752  0.34317802  0.72904971]
 [ 0.43857224  0.0596779   0.39804426  0.73799541  0.18249173  0.17545176]
 [ 0.53155137  0.53182759  0.63440096  0.84943179  0.72445532  0.61102351]]

Here we are getting a two-dimensional array with six-elements in every row.

Generating Random Numbers from Arrays

In order to generate a random number from arrays in NumPy, we have a method which is known as choice(). In this method, we are able to generate random numbers based on arrays which have various values. As a result, it takes the array and randomly chooses any number from that array.

Let us get through an example to understand it better:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.choice([4,5,6,7,8,9])
# here we will print the array 
print(a)

Output.

9

Here we are getting a random value from the array.

Let us get through another example where we will be using a size parameter to specify this shape of the array:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.choice([4,5,6,7,8,9], size=(3)) 
# here we will print the array 
print(a)

Output.

[9 6 8]

Here we are getting a random number in a one-dimensional array with some random numbers.

Let go through an example where we will try to build a two-dimensional array:

#importing the numpy package with random module 
from numpy import random 
# here we will use the random module 
a=random.choice([4,5,6,7,8,9], size=(3,4)) 
# here we will print the array 
print(a)

Output.

[[9 6 8 6]
 [5 7 6 7]
 [5 5 4 5]]

Here we are getting a 2-D dimensional array with four elements in each row. And it has three rows in total.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *