The randint() Function in PythonIn this tutorial, we will learn about the "randint()" function in Python. The "randint()" is a built-in function of the random module in Python. The random module is used for getting access to various functions like generating random numbers by using the randint() function. First, we have to import the random module in Python to use the randint() function. This function is basically used for creating pseudo-randomness. Syntax:Parameters:(start_range, end_range): Both the parameters must be integer type values. Parameters:It will return the random integer in the range [start_range, end_range], including both starting and ending numbers. Errors and Exceptions:ValueError: Returns a ValueError when the user passes floating point as parameters. TypeError: Returns a TypeError when the user passes anything other than integer value as parameters. Example 1:To get the random number between the given range of two positive numbers, two negative numbers, and one positive and one negative number. Output: 1# The any random number between 55 and 75 is 74 The any random number between -40 and -20 is -40 The any random number between -20 and 20 is -12 2# The any random number between 55 and 75 is 74 The any random number between -40 and -20 is -29 The any random number between -20 and 20 is -2 Example 2:In this example, we will see how the users can get the ValueError in the Python program while using the randint() function. Output: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Example 3:In this example, we will see how the user can get the TypeError in Python while using the randint() function. Output: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Applications:The users can use the randint() function for simulating a lucky draw game. Suppose we have participated in a lucky draw game such as "Casino Game". The player will get three chances to guess the number between 1 to 36. If we have guessed the number correctly, we will win, else we will lose the game. Example: code for application Output: 1# Please select your number to enter the lucky draw game 3 You have guessed Wrong Number!! Please select your number to enter the lucky draw game 2 You have guessed Wrong Number!! Please select your number to enter the lucky draw game 34 You have guessed Wrong Number!! Sorry, you have Lost the game! 2# Please select your number to enter the lucky draw game 14 You have guessed Wrong Number!! Please select your number to enter the lucky draw game 12 You have guessed Wrong Number!! Please select your number to enter the lucky draw game 3 Congratulation!! You have Won the game. ConclusionIn this tutorial, we have discussed the randint() function of Python's random module. We have shown the types of errors the user can get while using the randint() function. We have also discussed how the randint() function is used for creating an application of lucky draw games. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India