生成一个随机数一直是一个重要的应用,在日常生活中有很多用途。Python提供了一个函数,可以从指定的范围生成随机数,还允许包含步骤,称为 randrange() 在里面 随机的 单元本文将详细讨论这个函数。
null
Syntax : random.randrange(start(opt),stop,step(opt))Parameters :start(opt) : Number consideration for generation starts from this,default value is 0. This parameter is optional.stop : Numbers less than this are generated. This parameter is mandatory.step(opt) : Step point of range, this won't be included. This is optional.Default value is 1.Return Value : This function generated the numbers in the sequence start-stop skipping step.Exceptions :Raises ValueError if stop <= start and number is non- integral.
Python3
# Python code to demonstrate the working of # randrange() import random # Using randrange() to generate numbers from 0-100 print ( "Random number from 0-100 is : " ,end = "") print (random.randrange( 100 )) # Using randrange() to generate numbers from 50-100 print ( "Random number from 50-100 is : " ,end = "") print (random.randrange( 50 , 100 )) # Using randrange() to generate numbers from 50-100 # skipping 5 print ( "Random number from 50-100 skip 5 is : " ,end = "") print (random.randrange( 50 , 100 , 5 )) |
输出:
Random number from 0-100 is : 26Random number from 50-100 is : 58Random number from 50-100 skip 5 is : 90
例外情况
1.值错误–浮点值
Python3
# Python code to demonstrate the Exception of # randrange(), ValueError, Float value import random # Using randrange() to generate numbers from 14.5-100 # Raises Exception print ( "Random number from 14.5-100 is : " ,end = "") print (random.randrange( 14.5 , 100 )) |
输出:
Random number from 14.5-100 is :
运行时错误:
Traceback (most recent call last): File "/home/5e40f42505a6926d0c75a09bec1279d9.py", line 9, in print (random.randrange(14.5,100)) 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()
2. 值错误–开始>=停止
Python3
# Python code to demonstrate the Exception of # randrange(), ValueError, start >= start import random # Using randrange() to generate numbers from 500-100 # Raises Exception print ( "Random number from 500-100 is : " ,end = "") print (random.randrange( 500 , 100 )) |
输出:
Random number from 500-100 is :
运行时错误:
Traceback (most recent call last): File "/home/ea327cf3f1dd801a66a185d101c5cb13.py", line 9, in print (random.randrange(500,100)) File "/usr/lib/python3.5/random.py", line 196, in randrange raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))ValueError: empty range for randrange() (500,100, -400)
实际应用
生成随机数一直以来都是一个重要的应用,并被用于 许多赌场游戏,许多儿童游戏的赌博 比如ludo等等,它使用骰子的概念。 下面的代码描述了一个关于谁先赢得100场比赛的简短游戏。每位玩家可以掷1-10个数字的骰子,也就是说,每回合可以获得1-10个数字。 .
Python3
# Python code to demonstrate the Application of # randrange() import random sum = 0 sum1 = 0 count = 0 flag = 0 while ( 1 ): # generate random number at each turn 1-10 r1 = random.randrange( 1 , 10 ) r2 = random.randrange( 1 , 10 ) # adding to account of players sum = sum + r1 sum1 = sum1 + r2 count = count + 1 print ( "Total score of Player 1 after turn %d is : %d " % (count, sum )) # break when player 1 reaches 100 if ( sum > = 100 ): flag = 1 break print ( "Total score of Player 2 after turn %d is : %d" % (count,sum1)) # break when player 2 reaches 100 if (sum1> = 100 ): flag = 2 break if (flag = = 1 ): print ( "Player 1 wins the game" ) else : print ( "Player 2 wins the game" ) |
输出
Total score of Player 1 after turn 1 is : 8 Total score of Player 2 after turn 1 is : 4Total score of Player 1 after turn 2 is : 13 Total score of Player 2 after turn 2 is : 8Total score of Player 1 after turn 3 is : 22 Total score of Player 2 after turn 3 is : 16Total score of Player 1 after turn 4 is : 28 Total score of Player 2 after turn 4 is : 22Total score of Player 1 after turn 5 is : 33 Total score of Player 2 after turn 5 is : 27Total score of Player 1 after turn 6 is : 35 Total score of Player 2 after turn 6 is : 33Total score of Player 1 after turn 7 is : 36 Total score of Player 2 after turn 7 is : 42Total score of Player 1 after turn 8 is : 38 Total score of Player 2 after turn 8 is : 50Total score of Player 1 after turn 9 is : 45 Total score of Player 2 after turn 9 is : 55Total score of Player 1 after turn 10 is : 48 Total score of Player 2 after turn 10 is : 61Total score of Player 1 after turn 11 is : 54 Total score of Player 2 after turn 11 is : 64Total score of Player 1 after turn 12 is : 57 Total score of Player 2 after turn 12 is : 70Total score of Player 1 after turn 13 is : 66 Total score of Player 2 after turn 13 is : 73Total score of Player 1 after turn 14 is : 72 Total score of Player 2 after turn 14 is : 75Total score of Player 1 after turn 15 is : 79 Total score of Player 2 after turn 15 is : 76Total score of Player 1 after turn 16 is : 81 Total score of Player 2 after turn 16 is : 77Total score of Player 1 after turn 17 is : 89 Total score of Player 2 after turn 17 is : 81Total score of Player 1 after turn 18 is : 95 Total score of Player 2 after turn 18 is : 90Total score of Player 1 after turn 19 is : 97 Total score of Player 2 after turn 19 is : 99Total score of Player 1 after turn 20 is : 102 Player 1 wins the game
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END