# randrange () in python

Python Methods and Functions

Random number generation has always been an important application and has many uses in everyday life. Python offers a function that can generate random numbers from a given range, as well as allowing numbers for steps to be included in the cost, called randrange () in a random module. This feature is discussed in more detail 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 & lt; = start  and number is  non- integral . `

` `

` # Python code to demonstrate how it works # randrange ()   import random   # Using randrange () to generate numbers from 0 to 100 print ( "Random number from 0-100 is: " , end = " ") print (random.randrange ( 100 ))   # Using randrange () to generate numbers from 50 d about 100 print ( "Random number from 50-100 is: " , end = " ") print (random.randrange ( 50 , 100 ))   # Using randrange () to generate numbers between 50 and 100 # skip 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 `

Exceptions

1. Error value — floating point value

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

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 & gt; = stop

 ` # Python code to demonstrate the exception ` ` # randrange ( ), ValueError, start & gt; = start `   ` import ` ` random `   ` # Using randrange () to generate numbers from 500 to 100 ` ` # Raises the exception ` ` print ` ` (` ` "Random number from 500-100 is:" ` `, end ` ` = ` `" ") ` ` print ` ` (random.randrange (` ` 500 ` `, ` ` 100 ` `)) `

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

Random number generation has always been an important application and has been used in many casino games, for gambling in many children's games, such as ludo, etc., which use the Dice concept.  A short game about who reached the first 100 wins is shown in the code below. Each player is allowed to play dice from 1 to 10 numbers, i.e. 1-10 can be reached on each turn. ,

 ` # Python code to demonstrate usage ` ` # randrange () `   ` import ` ` random `   ` sum ` ` = ` ` 0 ` ` sum1 ` ` = ` ` 0 ` ` count ` ` = ` ` 0 ` ` flag ` ` = ` ` 0 ` ` `  ` while ` ` (` ` 1 ` ):   ` # generate a random number on every turn 1-10 ` ` r1 ` ` = ` ` random.randrange (` ` 1 ` `, ` ` 10 ` `) ` ` ` ` r2 ` ` = ` ` random.randrange (` ` 1 ` `, ` ` 10 ` `) `   ` # add players to your account ` ` 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 has reached 100 ` ` if ` ` (` ` sum ` ` & gt; ` ` = ` ` 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 & gt; ` ` = ` ` 100 ` `): ` ` flag ` ` = ` ` 2 ` ` break `   ` if ` ` (flag ` ` = ` ` = ` ` 1 ` `): ` ` print ` ` (` `" Player 1 wins the game "` `) ` ` else ` `: ` ` print ` ` (` ` "Player 2 wins the game" ` `) `

Output:

` 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 `

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.