Table of contents
1.
Introduction
2.
What is randrange()?
2.1.
Basic Usage
2.2.
Simple Random Number Generation:
2.3.
Parameters and Flexibility
3.
Examples
3.1.
Random Number Between Two Values
3.2.
Using Step for Even Numbers
4.
Practical Application
5.
Comparison with Other Random Functions
5.1.
randrange() vs. randint():
5.2.
randrange() for Sequences:
6.
Frequently Asked Questions
6.1.
Can randrange() return floating-point numbers?
6.2.
Is the output of randrange() truly random?
6.3.
Can I set a seed for randrange()?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

randrange() Python Function

Author Lekhika
0 upvote

Introduction

Random numbers play a pivotal role in various areas like simulations, cryptography, and gaming. Python, with its rich library ecosystem, provides the random module, packed with functions to generate random numbers. One such utility function is randrange(). This article dives deep into the randrange() function, guiding you through its purposes, usage, and nuances.

randrange() Python Function

What is randrange()?

The randrange() function is part of Python's random module. It returns a randomly chosen element from the given range. It's a versatile function that offers more flexibility than the similar randint() function, as it allows for a step parameter.

Basic Usage

Before using randrange(), it's essential to import the random module.

import random
You can also try this code with Online Python Compiler
Run Code

Simple Random Number Generation:

To generate a random number between 0 (inclusive) and 10 (exclusive):

print(random.randrange(10))
You can also try this code with Online Python Compiler
Run Code

 

It will generate any random number within the range of 0 to 10.
 

Parameters and Flexibility

The real power of randrange() lies in its flexible parameter list. The function signature is as follows:

random.randrange([start], stop[, step])

  • start: (Optional) Starting number of the sequence. Default is 0.
     
  • stop: (Mandatory) Upper limit of the sequence (exclusive).
     
  • step: (Optional) Difference between numbers in the sequence. Default is 1.

Examples

Here are basic examples of using randrange() Python function.

Random Number Between Two Values

To get a random number between 5 (inclusive) and 15 (exclusive):

import random
print(random.randrange(5, 15))
You can also try this code with Online Python Compiler
Run Code

Using Step for Even Numbers

To get a random even number between 0 and 10:

import random
print(random.randrange(0, 10, 2))
You can also try this code with Online Python Compiler
Run Code

Practical Application

Consider a simple dice-rolling simulation. A dice, when rolled, can give values from 1 to 6. Here's how you can simulate it using randrange():

import random
def roll_dice():
    return random.randrange(1, 7)
print(f"The dice rolled: {roll_dice()}")
You can also try this code with Online Python Compiler
Run Code

 

Output:

Output

Comparison with Other Random Functions

randrange() vs. randint():

Both these functions are used to retrieve random integers. While randrange() can take a step argument, randint() only takes the start and stop arguments and returns a random integer between those (inclusive of both).

random.randint(1, 6)  # Similar to the dice roll example
You can also try this code with Online Python Compiler
Run Code

randrange() for Sequences:

If you want a random choice from a list, tuple, or string, you might want to use random.choice() instead.

choices = ['apple', 'banana', 'cherry']
print(random.choice(choices))
You can also try this code with Online Python Compiler
Run Code

Also see,   reverse a string in python

Frequently Asked Questions

Can randrange() return floating-point numbers?

No, randrange() is designed to return integers. Use random.uniform() for float values.

Is the output of randrange() truly random?

Not exactly. It's pseudorandom, suitable for most applications but not for cryptographic purposes.

Can I set a seed for randrange()?

Yes, you can use random.seed() to initialize the random number generator, ensuring reproducibility.

Conclusion

The randrange() function, a part of Python's random module, is an indispensable tool for generating random integers within a range. Its flexibility in allowing a start, stop, and step parameter makes it versatile for various applications. Whether simulating a dice roll, choosing a random move in a game, or conducting Monte Carlo simulations, randrange() can be your go-to function for randomness in Python.

Live masterclass