Python中的randint()函数

2024 年 8 月 29 日 | 5 分钟阅读

在本教程中,我们将学习Python中的“randint()”函数。

“randint()”是Python中random模块的一个内置函数。random模块用于通过使用randint()函数来生成随机数等各种功能。

首先,我们必须在Python中导入random模块才能使用randint()函数。此函数主要用于创建伪随机性。

语法

参数

(start_range, end_range):这两个参数都必须是整数类型的值。

参数

它将在[start_range, end_range]范围内返回随机整数,包括起始和结束数字。

错误和异常

ValueError:当用户传入浮点数作为参数时,返回ValueError。

TypeError:当用户传入非整数值作为参数时,返回TypeError。

示例 1

要获取两个正数、两个负数或一个正数和一个负数给定范围内的随机数。

输出

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

示例 2

在此示例中,我们将看到用户在使用randint()函数时如何在Python程序中获得ValueError。

输出

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 
      4 # If the user passes any floating point values as the parameters in the randint() function.
      5 
----> 6 random_1 = rnd.randint(2.543, 12.786)
      7 print(random_1)

c:\users\User Name\appdata\local\programs\python\python39\lib\random.py in randint(self, a, b)
    336         """
    337 
--> 338         return self.randrange(a, b+1)
    339 
    340 

c:\users\user name\appdata\local\programs\python\python39\lib\random.py in randrange(self, start, stop, step)
    300         istart = int(start)
    301         if istart != start:
--> 302             raise ValueError("non-integer arg 1 for randrange()")
    303         if stop is None:
    304             if istart > 0:

ValueError: non-integer arg 1 for randrange()

示例 3

在此示例中,我们将看到用户在使用randint()函数时如何在Python中获得TypeError。

输出

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
      4 # If the user passes any string or character value as the parameters in the randint() function
      5 
----> 6 random_2 = rnd.randint('String', 'Character')
      7 print (random_2)

c:\users\user name\appdata\local\programs\python\python39\lib\random.py in randint(self, a, b)
    336         """
    337 
--> 338         return self.randrange(a, b+1)
    339 
    340 

TypeError: can only concatenate str (not "int") to str

应用

用户可以使用randint()函数来模拟抽奖游戏。

假设我们参加了一个抽奖游戏,例如“赌场游戏”。玩家将有三次机会猜1到36之间的数字。如果我们猜对了数字,我们将获胜,否则我们将输掉比赛。

示例:应用程序代码

输出

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.

结论

在本教程中,我们讨论了Python random模块的randint()函数。我们展示了用户在使用randint()函数时可能遇到的错误类型。我们还讨论了如何使用randint()函数来创建抽奖游戏的应用程序。