Random Number Generator in Python
Submitted by Yorkiebar on Monday, May 26, 2014 - 09:16.
Introduction:
This tutorial is on how to create a random number generator in Python.
Imports:
Before we begin, we first need to import the random module...
This allows us to use the random modules functions such as 'randrange' and 'randint'.
Random From Zero:
To get a random number from the beginning (zero/0), we can simply call the 'randrange' function from the random module and parse it a final/maximum value as an integer, like so...
The above code generates and outputs a random number between 0 and 100.
Random Between:
Or if we need to set a minimum value as well, we can use the 'randint' function. This function generates a random number between parameter 1 (minimum) and parameter 2 (maximum)...
The above code generates and outputs a random number between 20 and 100.
- import random
- print(random.randrange(100))
- print(random.randint(20, 100))
Add new comment
- 44 views