randrange() Python Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The randrange() function is a built-in Python function used to generate random integers between a given start and stop value. It is a part of the random module and can be used for various purposes like generating random passwords, selecting a random element from a list, or shuffling a deck of cards. The function takes at least one argument for the stop value, and can also take an optional argument for the start value and a step value. The function returns a random integer between the given range, with the start value being inclusive and the stop value being exclusive.

Syntax

The randrange is a function inside random module. So we have import that random module in our code to use this randrange() function. The syntax of randrange() function is given below. We can use this randrange() function only when we import the random module.

Parameters of randrange Python Function

Start, Stop, and Width are the three input parameters for the random.randrange() function. The two parameters start and width are the only two of these three that can be ignored. These 3 parameters are explained below.

  • A starting or lower limit number in a random range serves as the start parameter. Start takes the default value of 0 if it is not passed in as a parameter.
  • The last or highest number in a random range is referred to as a stop parameter.
  • A range between each number in the random sequence makes up the width parameter. If the width parameter is not provided, this optional parameter takes the default value of 1.

The end parameter is not used when the random integer number is generated by the randrange(start, stop, width) function. The stop parameter is excluded and is not produced by a random number generator.

Return Value of randrange Python Function

The randrange() function in Python returns a random integer from a range of values. The range is determined by the arguments passed to the function, with one argument generating a random integer between 0 and the stop value, two arguments generating a random integer between the start and stop values, and three arguments generating a random integer that is a multiple of the step value within the specified range. This function is useful for generating random integers in Python for various applications.

Exceptions of randrange Python Function

ValueError is raised if stop <= start and the integer is not integral. Let's understand it with an example given below.

Output

Now as given in the above example, the value of start>end so it is raising an ValueError. So it means we can't provide the start>end in randrange Python function otherwise it will raise ValueError.

Working of randrange Python Function

Within the start and stop points that are passed to the randrange() function, the random.randrange() function returns a random number. Start, Stop, and Width are the three parameters that the random.randrange() function accepts as input. The two parameters start and width are optional out of these three.

The end parameter is not used when the random integer number is generated by the randrange(start, stop, width) function. The stop parameter is unique and is not produced by a random number generator.

Consider the function random.randrange(2, 20, 2), which will produce any random integer value between 3 and 30 such as 2, 4, 6, 8,... . But when producing a random number, this algorithm never takes 20 into account.

Uses of randrange Python Function

  • It has always been essential to generate random numbers in applications since they have several real-time applications in daily life. An OTP (one-time password) is a random number that is generated and sent to a customer to facilitate a secure transaction.
  • When playing the game of Ludo, a random dice number is generated as an additional example of generating a randomly selected number.
  • In casino games, this random number generator is frequently used.
  • In online bike and car racing games, we have given a random number position, this random number generator is used.

Examples of randrange Python Function

Example 1: Generating a Rndom Integer Number Within the Given Range

Let's look at an instance where we generate a random integer number from a range. This example demonstrates every variation of the random.randrange() function.

Output

As we have generated 3 random number with all three different cases. For generating num1 we just have passed 50, which will be taken by the randrange function as end limit. The start paramater will be 0(by default) here. So the num1 will be between 0 and 50. And for generating num2 we have passed 20 as starting parameter and 50 as an ending parameter. These 2 are when passed to this randrange function it will return num2 which will be between 20 and 50. At last for generating the random number between 30 and 60 but also it should be the multiple of 4, we have passed 30 in place of parameterm 60 in place of ending parameter and 4 in place of width paramter.

Example 2: Generate a Random Integer Number within the Range (multiple) of n

Let's create a random number in the range of 10 to 90, such as 10, 20, 30,..., 80.

Output

In the example given above we want a random number between 10 and 90 but it should be a multiple of 10, so we will pass 10 as a width parameter and as a result randrange function will generate a randome number between 10 and 90 which is a multiple of 10.

Example 3: Generate a Random Integer Number with a Specific Length.

Additionally, you can produce random integers with a predetermined length. The randrange python function generates a random number of 3 digits from 100 to 999 where 4 digits (1000) is excluded, thus if you want to get a random number of length 3, enter the start and stop parameters as lowest number of 3 digit length (100) and least number of 4 digit length (1000).

Output

As we want a 3 digit number so we have pass starting limit which is of 3 digit itself and the ending limit should be the first 4 digit number, then only the random number which will be produced can be of 3 digit. So in the above example we have passed 100 as an starting limit(first 3 digit number) and 1000(first 4 digit number) as an end limit.

Example 4: Generate a Random Negative Integer Number.

A random negative integer value between -40 and -20 will be generated in the following example.

Output

In the example given above, both starting and ending limit of the range are passed negative to the randrange function. So it will return a any randome negative number that lies between -40 and -20.

Example 5: Generate Random Positive or Negative Integer Number

Output

As we can observe that range is given between -6 and 10, so when we passed these -6,10 inside the randrange function it have returned -3 in first time and on second time it have returned 6.

Conclusion

  • Any random integer number can be generated using randrange Python function.
  • Start and width parameters can be ignored by calling function.
  • Random generation of number is widely used in casino games.
  • Stop parameter value is excluded while generation of random number.