A random number is a number that cannot be predicted logically. A random number does not necessarily mean a different number each time.
Note: Any random number generated through any generation algorithm is called a pseudo-random number. This is because the algorithm can repeat the sequence, and the numbers are not entirely random.
Bearing that in mind, we can say that a program written to generate a random number is never truly random.
In this shot, we’ll take a look at how to generate pseudo-random numbers.
To generate a pseudo-random number in
NumPy, we will need the
random module offered by
In the code below, we will generate a pseudo-random integer between
# importing the random module from numpy from numpy import random # using the randit() method to generate random integers from 0 to 10 x = random.randint(10) # printing the random numbers print(x)
randint()function is used to generate pseudo-random integers.
Let’s take a look at another example.
In the code below, we want to use the
rand() function to generate a pseudo-random float number between
# importing the random module from numpy from numpy import random # using the rand() method to generate random integers from 0 to 10 x = random.rand(1) # printing the random float numbers print(x)
rand()function is used to generate pseudo-random float numbers.
View all Courses