randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions and one of them being able to generate random numbers, which is randint().
(start, end) : Both of them must be integer type values.
A random integer in range [start, end] including the end points.
Errors and Exceptions :
ValueError : Returns a ValueError when floating point values are passed as parameters. TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.
Code #1 :
Random number between 0 and 10 is 5 Random number between -10 and -1 is -7 Random number between -5 and 5 is 2
Code #2 : Program demonstrating the ValueError.
Traceback (most recent call last): File "/home/f813370b9ea61dd5d55d7dadc8ed5171.py", line 6, in r1=random.randint(1.23, 9.34) File "/usr/lib/python3.5/random.py", line 218, in randint return self.randrange(a, b+1) File "/usr/lib/python3.5/random.py", line 182, in randrange raise ValueError("non-integer arg 1 for randrange()") ValueError: non-integer arg 1 for randrange()
Code #3 : Program demonstrating the TypeError.
Traceback (most recent call last): File "/home/fb805b21fea0e29c6a65f62b99998953.py", line 5, in r2=random.randint('a', 'z') File "/usr/lib/python3.5/random.py", line 218, in randint return self.randrange(a, b+1) TypeError: Can't convert 'int' object to str implicitly
The randint() function can be used to simulate a lucky draw situation.
Let’s say User has participated in a lucky draw competition. The user gets three chances to guess the number between 1 and 10. If guess is correct user wins, else loses the competition.
Pick your number to enter the lucky draw 8 Wrong Guess!! Pick your number to enter the lucky draw 9 Wrong Guess!! Pick your number to enter the lucky draw 0 Congrats!! You Win.
- Random sampling in numpy | randint() function
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- wxPython - GetField() function function in wx.StatusBar
- How to write an empty function in Python - pass statement?
- Function Decorators in Python | Set 1 (Introduction)
- Vulnerability in input() function – Python 2.x
- Function Annotations in Python
- Sorted() function in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python Numbers | choice() function
- Python | askopenfile() function in Tkinter
- Python | Binding function in Tkinter
- floor() and ceil() function Python
- Sum 2D array in Python using map() function
- ord() function in Python
- sum() function in Python
- round() function in Python
- id() function in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : kaladarchowdary