A random number does not necessarily imply a different value each time; rather, random refers to something that cannot be predicted logically.
Computers run on programs that consist of defined instructions, requiring an algorithm to generate random numbers. Since these numbers can be predicted, they are considered pseudo-random.
To create truly random numbers, external sources of randomness, such as keystrokes, mouse movements, or network data, are needed. Truly random numbers are mainly used for security (e.g., encryption keys) or applications that rely on randomness (e.g., digital roulette wheels). This tutorial will focus on using pseudo-random numbers.
NumPy provides the random module for working with random numbers.
Create a random integer between 0 and 100:
from numpy import random x = random.randint(100) print(x) |
The rand() method from the random module returns a random float between 0 and 1.
Create a random float between 0 and 1:
from numpy import random x = random.rand() print(x) |