The Python random.randrange() method returns a randomly selected element from the given range.
This method accepts two parameters which are start, stop. Therefore, it generates random numbers between the start value and the end value of the range.
This function is not accessible directly, so we need to import random module and then we need to call this function using random static object.Syntax
Following is the syntax of Python random.randrange() method −
random.randrange ([start,] stop [,step])Parameters
start − Start point of the range. This would be included in the range. By default it is 0.
stop − Stop point of the range. This would be excluded from the range.
step − Steps to be added in a number to decide a random number. By default it is 1.
This method returns a random item from the given range.
Example 1The following example shows the usage of the Python random.randrange() method. Here the start value, the end value and the step size to be added in the number is passed as an argument to this method.
import random # Select an even number in 100 <= number < 1000 print ("randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)) # Select another number in 100 <= number < 1000 print ("randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3))
When we run above program, it produces following result −
randrange(100, 1000, 2) : 976 randrange(100, 1000, 3) : 520Example 2
In here, only the start value and the end value is passed as an argument to the randrange() method. We are not providing the step size to be added in the number.:
import random Start = 25 End = 45 randNo = random.randrange(Start,End) print('The random number generated is:', randNo)
Following is an output of the above code −
The random number generated is: 32Example 3
In the example given below we are creating the start value '25' and end value '45'. Then we are assigning a value to the total random numbers we need to generate in the list.
Thereafter, a for loop is used to generate n random numbers. Inside this for loop we append each random number to the list. Then we invoke the randrange() method to generate random numbers.
import random Start = 25 End = 45 TotalRandomNumbers = 5 List = [] for i in range(TotalRandomNumbers): randNo = random.randrange(Start, End) List.append(randNo) print('The random numbers list is:', List)
While executing the above code we get the following output −
The random numbers list is: [36, 36, 42, 35, 26]Example 4
If we pass a float value as an argument to this method, it raises a ValueError.
import random Start = 25.54 End = 45.67 randNo = random.randrange(Start,End) print('The random number generated is:', randNo)
Output of the above code is as follows −
/home/cg/root/77837/main.py:4: DeprecationWarning: randrange() will raise TypeError in the future randNo = random.randrange(Start,End) Traceback (most recent call last): File "/usr/lib/python3.10/random.py", line 303, in randrange istart = _index(start) TypeError: 'float' object cannot be interpreted as an integer During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/cg/root/77837/main.py", line 4, in <module> randNo = random.randrange(Start,End) File "/usr/lib/python3.10/random.py", line 309, in randrange raise ValueError("non-integer arg 1 for randrange()") ValueError: non-integer arg 1 for randrange()
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4