Python random.randrange()

Python random.randrange()

Generating random numbers has always been an important application, with many uses in everyday life. Python provides a function that can generate random numbers from a specified range, including step ranges. It is called randrange() in the random module. This function will be discussed in more detail in this article.

Syntax:
random.randrange(start(opt),stop,step(opt))
Parameters:
start(opt): Numbers to consider start at this point. The default value is 0. This parameter is optional.
stop: Numbers less than this point will be generated. This parameter is mandatory.
step(optional): Step point in the range that will not be included. This is optional. The default value is 1.
Return Value:
This function generates the numbers in the sequence start-stop, skipping steps.
Exceptions:
If stop <= start and number is a non-integer, a ValueError is raised.

# Python code to demonstrate the workings 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))

Output.

Random number from 0-100 is : 26
Random number from 50-100 is : 58
Random number from 50-100 skip 5 is : 90

Exception

1. Numerical error – floating point value

# Python code to demonstrate the Exception of
# randrange(), ValueError, Float value
 
import random
 
# Using randrange() to generate numbers from 14.5-100
#RaisesException
print ("Random number from 14.5-100 is : ",end="")
print (random.randrange(14.5,100))

Output:

Random number from 14.5-100 is :

Run-time error:

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. Numerical error – Start >= Stop

# Python code to demonstrate the Exception of
# randrange(), ValueError, start >= start
 
import random
 
# Using randrange() to generate numbers from 500-100
#RaisesException
print ("Random number from 500-100 is : ",end="")
print (random.randrange(500,100))

Output:

Random number from 500-100 is :

Run-time error:

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)

Practical Applications

Random number generation has always been an important application and is used in many casino games and children’s gambling games, such as Ludo, which utilize dice. A short game where the first player to reach 100 wins is described in the code below. Each player is allowed to use dice with numbers 1-10, meaning they can roll 1-10 on each turn.

# 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")

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *