Generate a random number in Python
This post will discuss how to generate a random number in Python.
1. Using random.randrange() function
A simple approach to generate a random number between the specified range in Python is using the random.randrange() function. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 |
import random if __name__ == '__main__': start = 1 end = 5 # print random number between start (inclusive) & end (exclusive) print(random.randrange(start, end)) |
To generate a random element less than a value, you can use the following code:
|
1 2 3 4 5 6 7 |
import random if __name__ == '__main__': # print random number between 0 and 9, both inclusive print(random.randrange(10)) |
2. Using random.randint() function
The random.randrange(x, y) function generates the random integer n such that x <= n < y. If you need to generate a random number inclusive of the end argument (i.e., x <= n <= y), use the random.randint(x, y) function.
In other words, random.randint(x, y) function is equivalent for random.randrange(x, y+1).
|
1 2 3 4 5 6 7 8 9 10 |
import random if __name__ == '__main__': start = 1 end = 5 # print random number between start & end, both inclusive print(random.randint(start, end)) |
3. Using random.choice() function
The randint() or randrange() is the preferred way to generate a random number from a continuous sequence. To pick a random item from a sequence like a list or a tuple, you should use the random.choice() function.
|
1 2 3 4 5 6 7 8 9 10 |
import random if __name__ == '__main__': values = list(range(1, 10)) # print a random number from a specified list rand = random.choice(values) print(rand) |
4. Using secrets.randbelow() function
The random module is used to generate pseudorandom numbers. To generate a cryptographically secure random number, consider using the randbelow() function from the secrets module.
|
1 2 3 4 5 6 7 8 |
import secrets if __name__ == '__main__': # print cryptographically secure random number # between 0 and 9, both inclusive print(secrets.randbelow(10)) |
5. Using secrets.choice() function
The secrets.randbelow() function is preferred to cryptographically secure random numbers from a continuous sequence. To generate a cryptographically secure random number from a sequence like a list or a tuple, use the secrets.choice() function.
|
1 2 3 4 5 6 7 8 9 10 |
import secrets if __name__ == '__main__': values = list(range(1, 10)) # print cryptographically secure random number from a specified list rand = secrets.choice(values) print(rand) |
6. Using numpy.random.randint() function
With NumPy, you may use the numpy.random.randint(x, y) function.
|
1 2 3 4 5 6 7 8 9 |
import numpy as np if __name__ == '__main__': start = 1 end = 5 print(np.random.randint(start, end)) |
That’s all about generating a random number in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)