 # randrange() in Python

## 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 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.

```

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

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

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

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

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

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

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

```

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Last Updated on March 1, 2022 by admin

## break, continue and pass in Pythonbreak, continue and pass in Python

break, continue and pass in Python Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that

## Python – Check if all the values in a list that are greater than a given valuePython – Check if all the values in a list that are greater than a given value

Python | Check if all the values in a list that are greater than a given value Given a list, print all the values in a list that are greater than the given value Examples: Input : list = [10,

## Python – Sentiment Analysis using VADERPython – Sentiment Analysis using VADER

Python | Sentiment Analysis using VADER Sentiment Analysis is the process of ‘computationally’ determining whether a piece of writing is positive, negative or neutral. It’s also known as opinion mining, deriving the opinion or attitude of a speaker. Why sentiment analysis? Business: In

## How to Filter DataFrame Rows Based on the Date in Pandas?How to Filter DataFrame Rows Based on the Date in Pandas?

How to Filter DataFrame Rows Based on the Date in Pandas? Different regions follow different date conventions (YYYY-MM-DD, YYYY-DD-MM, DD/MM/YY, etc.).  It is difficult to work with such strings in the data. Pandas to_datetime() function allows converting the date and time in string

## How to get file size in Python?How to get file size in Python?

How to get file size in Python? We can follow different approaches to get the file size in Python. It’s important to get the file size in Python to monitor file size or in case of ordering files in the

## Python program to input a comma separated stringPython program to input a comma separated string

Python program to input a comma separated string Given an input string that is comma-separated instead of space. The task is to store this input string in a list or variables. This can be achieved in Python using two ways:

## Remove all the occurrences of an element from a list in PythonRemove all the occurrences of an element from a list in Python

Remove all the occurrences of an element from a list in Python The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Examples :   Input : 1 1 2

## Python – Get list of running processesPython – Get list of running processes

Python – Get list of running processes A Process is a program that is being executed (processed). A process may not have to be one run explicitly by the user, it could be a system process spawned by the operating system. Any