Generating a random number has always been an important application and having many uses in daily life. Python offers a function that can generate random numbers from a specified range and also allowing rooms for steps to be included, called randrange() in random module. More to this function is discussed in this article.
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
import random
print ("Random number from 500-100 is : ",end="")
print (random.randrange(500,100))
|
Output:
Random number from 0-100 is : 26
Random number from 50-100 is : 58
Random number from 50-100 skip 5 is : 90
Exceptions
1. Value Error – Floating point value
Python3
import random
sum = 0
sum1 = 0
count = 0
flag = 0
while(1):
r1 = random.randrange(1,10)
r2 = random.randrange(1,10)
sum = sum + r1
sum1 = sum1 + r2
count = count+1
print ("Total score of Player 1 after turn %d is : %d " % (count,sum))
if(sum>=100):
flag=1
break
print ("Total score of Player 2 after turn %d is : %d" % (count,sum1))
if(sum1>=100):
flag=2
break
if(flag==1):
print("\nPlayer 1 wins the game")
else :
print("\nPlayer 2 wins the game")
|
Output :
Random number from 14.5-100 is :
Runtime 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. Value Error – start >= stop
Output :
Random number from 500-100 is :
Runtime 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 Application
Generating random numbers has always been an important application and has been used in many casino games, for gambling for many kid games like ludo, etc which use the concept of Dice. A short game, on who reaches 100 first wins have been depicted in the code below. Each player is allowed a dice of 1-10 numbers, i.e at each turn 1-10 can be attained..
Python3
import random
sum = 0
sum1 = 0
count = 0
flag = 0
while(1):
r1 = random.randrange(1,10)
r2 = random.randrange(1,10)
sum = sum + r1
sum1 = sum1 + r2
count = count+1
print ("Total score of Player 1 after turn %d is : %d " % (count,sum))
if(sum>=100):
flag=1
break
print ("Total score of Player 2 after turn %d is : %d" % (count,sum1))
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
Similar Reads
Python Random Module
Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python. List
7 min read
Python - random.seed( ) method
random() function is used to generate pseudo-random numbers in Python. That implies that these randomly generated numbers can be determined. random() function generates numbers for some values. This value is also called seed value. Here we will see how we can generate the same random number every ti
3 min read
random.getstate() in Python
random() module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getstate() The getstate() method of the random module returns an object with the curr
1 min read
random.setstate() in Python
Random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.setstate() The setstate() method of the random module is used in conjugation with the g
2 min read
random.getrandbits() in Python
random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getrandbits() The getrandbits() method of the random module is used to return an intege
1 min read
randrange() in Python
Generating a random number has always been an important application and having many uses in daily life. Python offers a function that can generate random numbers from a specified range and also allowing rooms for steps to be included, called randrange() in random module. More to this function is dis
6 min read
randint() Function in Python
randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python. Python randint() Method SyntaxSyntax: randint(st
6 min read
Python Numbers | choice() function
choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to im
1 min read
Python - random.choices() method
The choices() method returns multiple random elements from the list with replacement. Unlike random.choice(), which selects a single item, random.choices() allows us to select multiple items making it particularly useful for tasks like sampling from a population or generating random data. Example: [
3 min read
Python | random.sample() function
sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Syntax : random.sample(sequence, k) Parameters:sequence: Can be a list, tuple, string, or
2 min read