Python random randrange














































Python random randrange



Python random randrange() 


In this article, we will see how to use Python 
random.randrange() function.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.  
This function takes three parameters. Out of three parameterstwo parameters are optional.

Syntax :

random.randrange(startstopstep)


Parameter Values

ParameterDescription
startOptional. An integer specifying at which position to start.
Default 0
stopRequired. An integer specifying at which position to end.
stepOptional. An integer specifying the incrementation.
Default 1


Return Value:

This function generated the numbers in the sequence start-stop skipping step

 Note:

The randrange(start, stop, step) doesn't include the stop number while generating random integer, i.e., it is exclusive.

For example, random.randrange(2, 20, 2) will return any random number between 2 to 20, such as 2, 4, 6,18. It will never select 20.


Here in the following example, we are trying to print a random int in a given range. This example demonstrates all the variants of random.randrange() function.


#import the random module
import random

print("Generate random number within a given range in Python ")

# Random number between 0 and 39
# with only "stop" parameter
num1 = random.randrange(100)
print("Random integer 1 : ", num1)

# Random number between 100 and 500
# with only "start" and "stop" parameters
num2 = random.randrange(100500)
print("Random integer 2 : ", num2)

# Random number between 25 and 249 divisible by 5
# with all of the parameters used
num3 = random.randrange(252505)
print("Random integer 3 : ", num3)
Output : 
Random integer 1 :  56
Random integer 2 :  476
Random integer 3 :  135 


 Note:

You should be aware of some value constraints of a randrange() function.

  • The randrange() only works with integers. You cannot use float numbers.
  • If you use float numbers it will raise ValueError: non-integer arg 1 for randrange()
  • The start should not be greater than stop. If mistakenly you set start greater than stop you will get a ValueError: empty range for randrange()
  • The step must not be 0. If it is set to 0, you will get a ValueError: zero step for randrange()

Examples :

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 60.5-1000
# Raises Exception 
print ("Random number from 60.5-1000 is : ",end=""
print (random.randrange(60.5,1000)) 

Output :




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 :




           *******END OF ARTICLE*******



More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 114 0
Python string swapcase 92 0
Python string title 96 0
Python string startswith 102 0
Python string replace 144 0
Python string translate 84 0
Python string rpartition 85 0
Python string partition 74 0
Python string splitlines 113 0
Python string rsplit 79 0
Python string split 75 0
Python string rindex 81 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 81 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 91 0
Python string lstrip 79 0
Python string rjust 96 0
Python string ljust 95 0
Python string len 81 0
Python string join 75 0
Python string casefold 82 0
Python string isprintable 84 0
Python string encode 78 0
Python string isdecimal 84 0
Python string isidentifier 78 0
Python string isupper 77 0
Python string istitle 79 0
Python string isspace 100 0
Python string isnumeric 101 0
Python string isdigit 82 0
Python string islower 84 0
Python string isalpha 95 0
Python string isalnum 101 0
Python string index 87 0
Python string find 93 0
Python string expandtabs() 80 0
Python string endswith 91 0
Python string count 86 0
Python string capitalize 77 0
Python string center 84 0
Python string Introduction 98 0
Python string Template 139 0
Python string Formatter 144 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 103 0
Python string octdigits 86 0
Python string hexdigits 103 0
Python string digits 79 0
Python string ascii_letters 344 0
Python string ascii_lowercase 146 0
Python string ascii_uppercase 156 0
How to Create Download Manager in Python 523 0
Python random weibullvariate 164 0
python random paretovariate 133 0
Python random vonmisesvariate 178 0
Python random normalvariate 195 0
Python random lognormvariate 119 0
Python random gauss 120 0
Python random gammavariate 131 0
Python random expovariate 194 0
Python random betavariate 184 0
Python random triangular 116 0
Python random uniform 140 0
Python random random 116 0
Python random sample 182 0
Python random shuffle 178 0
Python random choices 234 0
Python random choice 165 0
Python random randint 148 0
Python random randrange 214 0
Python random getrandbits 140 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0

Comments