Built with Tutify.io

    AIStudyLabs

    Python Random randrange() Function

    Python Random randrange() Function

    Python provides the randrange() function from the random module to generate a random number between the given range of integral numbers along with the step value. It takes three parameters start, stop, and step, and it returns random numbers between the start and stop(excluding) along with a specified interval.

    You can use this method with variations of signatures random.randrange(stop) and random.randrange(start, stop[, step]) that generate random integers from a range.

    The first form random.randrange(stop) returns a random integer from the range [0, stop), meaning that the result will be greater than or equal to 0, but less than stop.

    In this article, I will explain Python random.randrange() function syntax, parameters, and usage of how we can generate the random integer within a given range by specifying step value.

    <a id="h-1-quick-examples-of-randrange-function"></a>

    1. Quick Examples of randrange() Function

    If you are in a hurry, below are some quick examples of the randrange() function.

    
    #  Quick examples of randrange() function.
    
    # Example 1: Generate random number between 1 to 10
    # using randrange()
    random_value= random.randrange(1, 10, 2)
    
    # Example 2: Generate random integer from 1 to 100 
    # using randrange()
    random_value= random.randrange(1, 100, 10)
    
    # Example 3: Generate random integer of negative using randrange()
    random_value= random.randrange(-20, -10)
    
    # Example 4: Generate positive/negative random 
    # integer using randrange()
    for i in range(3):
        random_value= random.randrange(-20, 10)
        print(random_value)
    

    <a id="h-2-python-random-randrange-function"></a>

    2. Python random.randrange() Function

    The randrange(start,stop,step) from a random module is used to generate the random number between the given range with specified step. It takes start and end numeric values as its parameters, so that a random number is generated within this specified range.

    random.randrange(stop) returns a random integer from the range [0, stop), meaning that the result will be greater than or equal to 0, but less than stop.

    <a id="h-2-1-syntax-of-randrange-function"></a>

    2.1 Syntax of randrange() Function

    Following is the syntax of the randrange() Function.

    
    # Syntax of randrange() function
    random.randrange(start, stop[, step])
    

    <a id="h-2-2-parameter-of-randrange"></a>

    2.2 Parameter of randrange()

    <a id="h-2-3-return-value"></a>

    2.3 Return Value

    It returns randomly generated numbers between the given range along with specified increments.

    <a id="h-3-get-random-number-between-1-to-10-range-in-python"></a>

    3. Get Random Number Between 1 to 10 Range in Python

    Let's use the Python random.randrange() function to generate random numbers from specified range of intervals. In the below example, we get the random integer between 1 to 10(exclude) with step value '2'.

    Related:Generate Random Integers Between 0 and 9 in Python

    
    import random
    
    # Generate random number between 1 to 10
    # using randrange()
    random_value= random.randrange(1, 10, 2)
    print("Random number between 1 to 10:", random_value)
    

    Yields below output.

    <a id="h-4-get-python-random-number-between-1-to-100-range"></a>

    4. Get Python Random Number Between 1 to 100 Range

    Let's see another example of getting the random number between the range of 1 to 100 with step value 10.

    
    import random
    
    # Generate random integer from 1 to 100 
    # using randrange()
    random_value= random.randrange(1, 100, 10)
    print("Random number between 1 to 100:", random_value)
    

    Yields below output.

    <a id="h-5-generate-a-random-negative-integer-from-range-in-python"></a>

    5. Generate a random negative integer from Range in Python

    Alternatively, we can get the negative random number by providing a range of negative numbers into randrange() function.

    
    import random
    
    # Generate random integer of negative using randrange()
    random_value= random.randrange(-20, -10)
    print("Random integer:", random_value)
    
    # Output:
    # Random integer: -15
    

    We can also get the random number of positive/negative using the randrange() function. Providing a range of numbers with positive(stop) and negative (start) into the randrange() function will generate either a positive random number or a negative random number.

    Let's run the code multiple times and to generate multiple random numbers.

    
    import random
    
    # Generate positive/negative random 
    # integer using randrange()
    print("Random Integer:")
    for i in range(3):
        random_value= random.randrange(-20, 10)
        print(random_value)
    
    # Output:
    # Random Integer:
    # -12
    # -15
    #  9
    

    <a id="h-6-generate-random-float-number"></a>

    6. Generate Random Float Number

    randrange() doens't support floating values, by using the floats it raises the ValueError.

    
    import random
    
    # Generate random integer using randrange()
    random_value= random.randrange(15, 20.2)
    print("Random number of float:", random_value)
    
    # Output: ValueError: non-integer stop for randrange()
    

    <a id="h-7-conclusion"></a>

    7. Conclusion

    In this article, I have explained Python random.randrange() function syntax, parameters, and usage of how to generate the random integer within a given range by specifying step value. And also explained when we pass non-integral numbers into this function it generates a ValueError.

    Happy Learning!!

    References

    <a id="h-related-articles"></a>

    Related Articles