Python random.randrange()

Python random.randrange()

生成一个随机数一直是一个重要的应用,在日常生活中有着许多用途。Python提供了一个函数,可以从指定的范围内生成随机数,也允许包含步骤的房间,在随机模块中称为randrange()。对这个函数的更多内容将在本文中讨论。

语法:
random.randrange(start(opt),stop,step(opt))
参数:
start(opt) : 代号的考虑是从这个开始的, 默认值为0,这个参数是可选的
stop : 小于此的数字会被生成。这个参数是强制性的
step(可选) : 步骤点的范围,这不会被包括在内。这是可选的。默认值为1.
返回值 :
这个函数生成了序列中的数字,start-stop,跳过步骤.
异常:
如果stop <= start且数字为非整数,则引发ValueError.

# 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 :  8 
Total score of Player 2 after turn 1 is :  4
Total score of Player 1 after turn 2 is :  13 
Total score of Player 2 after turn 2 is :  8
Total score of Player 1 after turn 3 is :  22 
Total score of Player 2 after turn 3 is :  16
Total score of Player 1 after turn 4 is :  28 
Total score of Player 2 after turn 4 is :  22
Total score of Player 1 after turn 5 is :  33 
Total score of Player 2 after turn 5 is :  27
Total score of Player 1 after turn 6 is :  35 
Total score of Player 2 after turn 6 is :  33
Total score of Player 1 after turn 7 is :  36 
Total score of Player 2 after turn 7 is :  42
Total score of Player 1 after turn 8 is :  38 
Total score of Player 2 after turn 8 is :  50
Total score of Player 1 after turn 9 is :  45 
Total score of Player 2 after turn 9 is :  55
Total score of Player 1 after turn 10 is :  48 
Total score of Player 2 after turn 10 is :  61
Total score of Player 1 after turn 11 is :  54 
Total score of Player 2 after turn 11 is :  64
Total score of Player 1 after turn 12 is :  57 
Total score of Player 2 after turn 12 is :  70
Total score of Player 1 after turn 13 is :  66 
Total score of Player 2 after turn 13 is :  73
Total score of Player 1 after turn 14 is :  72 
Total score of Player 2 after turn 14 is :  75
Total score of Player 1 after turn 15 is :  79 
Total score of Player 2 after turn 15 is :  76
Total score of Player 1 after turn 16 is :  81 
Total score of Player 2 after turn 16 is :  77
Total score of Player 1 after turn 17 is :  89 
Total score of Player 2 after turn 17 is :  81
Total score of Player 1 after turn 18 is :  95 
Total score of Player 2 after turn 18 is :  90
Total score of Player 1 after turn 19 is :  97 
Total score of Player 2 after turn 19 is :  99
Total score of Player 1 after turn 20 is :  102 

Player 1 wins the game

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程