当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python randrange()用法及代码示例


产生随机数一直是重要的应用,并且在日常生活中有许多用途。 Python提供了一个函数,该函数可以在指定范围内生成随机数,还可以为要包含的步骤留出空间,在随机模块中称为randrange()。本文讨论了更多有关此函数的信息。

用法:
random.randrange(start(opt),stop,step(opt))
参数:
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.
返回值: 
This function generated the numbers in the sequence start-stop skipping step.
Exceptions:
Raises ValueError if stop <= start and number is non- integral.
# 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:26
Random number from 50-100 is:58
Random number from 50-100 skip 5 is:90
异常

1.值错误-浮点值


# 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.值错误-开始> =停止

# 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个骰子。

# 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("\nPlayer 1 wins the game") 
else :
   print("\nPlayer 2 wins the game")   

输出:

Total score of Player 1 after turn 1 is: 9 
Total score of Player 2 after turn 1 is: 8
Total score of Player 1 after turn 2 is: 15 
Total score of Player 2 after turn 2 is: 10
Total score of Player 1 after turn 3 is: 16 
Total score of Player 2 after turn 3 is: 12
Total score of Player 1 after turn 4 is: 23 
Total score of Player 2 after turn 4 is: 13
Total score of Player 1 after turn 5 is: 26 
Total score of Player 2 after turn 5 is: 16
Total score of Player 1 after turn 6 is: 33 
Total score of Player 2 after turn 6 is: 23
Total score of Player 1 after turn 7 is: 41 
Total score of Player 2 after turn 7 is: 31
Total score of Player 1 after turn 8 is: 43 
Total score of Player 2 after turn 8 is: 39
Total score of Player 1 after turn 9 is: 51 
Total score of Player 2 after turn 9 is: 45
Total score of Player 1 after turn 10 is: 56 
Total score of Player 2 after turn 10 is: 50
Total score of Player 1 after turn 11 is: 57 
Total score of Player 2 after turn 11 is: 59
Total score of Player 1 after turn 12 is: 60 
Total score of Player 2 after turn 12 is: 66
Total score of Player 1 after turn 13 is: 64 
Total score of Player 2 after turn 13 is: 68
Total score of Player 1 after turn 14 is: 69 
Total score of Player 2 after turn 14 is: 76
Total score of Player 1 after turn 15 is: 71 
Total score of Player 2 after turn 15 is: 80
Total score of Player 1 after turn 16 is: 72 
Total score of Player 2 after turn 16 is: 85
Total score of Player 1 after turn 17 is: 76 
Total score of Player 2 after turn 17 is: 87
Total score of Player 1 after turn 18 is: 78 
Total score of Player 2 after turn 18 is: 94
Total score of Player 1 after turn 19 is: 79 
Total score of Player 2 after turn 19 is: 102

Player 2 wins the game



相关用法


注:本文由纯净天空筛选整理自 randrange() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。