Generating random number is often required when developing an application. Examples might be creating a number guessing game, or designing card games where random card needs to be picked up from deck or generating tokens for sending in HTTP requests etc.

Python supports generating random numbers, both integers and decimal numbers within a defined range. Its random module provides various utility functions to generate random numbers.

This post covers all ways in which a random number can be generated in python.

**Generating Integers**

Integers means whole numbers which do not contain a decimal such as 0, 3, 7, 19, 99, 256 etc. Following functions can be used to generate random integers in python. Note that for all these functions to work, you need to import Python’s ** random** module.

**1. randint**

This method takes two arguments(both integers) that define the range in which random number will be generated. Generated number will be between the provided arguments with both of them inclusive. That is, if a and b are the integers supplied to this method, then the number generated will be a <= N <= b. Example, to generate random numbers between 1 and 20, following code would be useful.

import random # generate a random number random_number = random.randint(1, 20) print("Random number is " + str(random_number)) # generate a random number again random_number = random.randint(1, 20) print("Another random number is " + str(random_number))

It is necessary to provide 2 arguments to `randint`

function. If you provide 0 or 1 arguments to it, then you will get following error

TypeError: randint() missing 2 required positional arguments: ‘a’ and ‘b’

TypeError: randint() missing 1 required positional argument: ‘b’

**2. randrange**

This function can also be used to generate random integers. There are two variants of this function,

**randrange(stop) :**takes a single integer argument which represents the upper limit of the random numbers. That is, the numbers generated will start from 0 to supplied argument – 1.**randrange(start, stop, [, step]) :**takes at least 2 integer arguments which represent the start and end range of generated random numbers. Third argument is optional but if it is provided, it reduces the random numbers that are generated. Random numbers that are generated are among the numbers formed by adding`step`

value to the`start`

value. They will always lie between`start`

and`stop`

values with`start`

value included and`stop`

value excluded. Thus,

`randrange(0, 10, 2)`

will only generate random numbers among 0, 2, 4, 6 and 8.

`randrange(1, 10, 3)`

will only generate random numbers among 1, 4 and 7.

`randrange(0, 101, 2)`

will generate only even numbers till 100.

From the above examples, it is clear that`randrange`

can be easily used to generate random odd numbers and random even number with in a given range in python.

Example,

import random print(random.randrange(5)) # will generate numbers from 0 to 4 print(random.randrange(2, 50)) # will generate numbers from 2 to 49 # will generate random numbers from among # 2, 12, 22, 32 and 42 print(random.randrange(2, 50, 10))

```
import randint from random
randint(1, 20) # call function directly
```

step value supplied to function randrange is optional but if it is provided, it should be greater than 0 else an error will be raised. Thus, step value of 0 will result in an error as below

ValueError(“zero step for randrange()”)

**Generating random decimal numbers**

Following are the functions used for generating random decimal numbers in python.

**1. random**

This function does not take any arguments and generates random numbers between 0.0 and 1.0.

Example,

import random # generate random decimal between 0 and 1 random_decimal = random.random() print("Result of random function = " +str(random_decimal))

Output of above program will be

Result of random function = 0.24693110760098724

**2. uniform**

It takes two arguments which represent the start and end boundaries between which the random decimal will be generated. Start and end arguments may be integers or decimal numbers.

Example,

import random # generate random decimal between 1 and 5 random_decimal = random.uniform(1,5) print("Result of uniform function = " +str(random_decimal))

Output of above program will be

Result of uniform function = 1.9261172442609968

## Leave a Reply