# Random Number Generator in Python

In this article, we will learn how to generate random numbers in Python. You will see random number generator in Python with examples.

There are situations when you need some random numbers to test your program or to check the behavior of a model. In such a scenario, you need a random number to perform the task.

## Quick Learning

If you are in hurry or you want to quickly go through random number generation in Python then look at the following code snippet.

There is one thing to note here. Python requires the **random** module to generate random numbers. So first import the random module.

```
import random
# random number between 0 and 1
num = random.random()
print(num)
# random integer between 1 and 10
num = random.randint(1, 10)
print(num)
# random number between 1 and 10 with floating point
num = random.uniform(1, 10)
print(num)
# choose a random element from a list, tuple, string, etc.
list = [1, 2, 3, 4, 5]
num = random.choice(list)
print(num)
# shuffle a list, tuple, string, etc.
list = [1, 2, 3, 4, 5]
random.shuffle(list)
print(list)
```

Output: (may vary)

0.436671069889 8 3.0013320188 1 [2, 3, 5, 4, 1]

## Python Random Number Between 1 and 10

The random module has a function randint that generates a random number (**integer**) between any two numbers.

The function takes two arguments, one is the lower bound and the other is the upper bound.

To generate a random number between 1 and 10, pass 1 and 10 as arguments to the function.

```
import random
# random number between 1 and 10
num = random.randint(1, 10)
print(num)
```

Output: (may vary)

7

## Functions for Random Number Generation

The random module has lots of function that help us with different random number generation tasks.

The following table lists the functions available in the random module for random number generation.

Function | Description |
---|---|

random() | Generates a random number between 0 and 1. 1 is not included in the range. |

randint(a, b) | Generates a random number between a and b (both inclusive). Generated numbers are integer values. |

uniform(a, b) | Generates a random number between a and b (both inclusive). Generated numbers are floating point values. |

randrange(start, stop, step) | Generates a random number between start and stop (both inclusive). The step argument is optional. If it is not provided, the default value is 1. |

choice(seq) | Chooses a random element from a sequence. The sequence can be a list, tuple, string, etc. |

sample(seq, k) | Chooses k unique random elements from a sequence. The sequence can be a list, tuple, string, etc. |

shuffle(seq) | Shuffles the elements of a sequence and changes it into a random order. |

## 1. random() Function

The random() Python function generates a floating point random number between 0 and 1.

0 is included in the range and 1 is not included.

```
import random
# random function
# generates a random number between 0 and 1
num = random.random()
print(num)
```

Output: (may vary)

0.436671069889

## 2. randint() Function

The randint() function generates a random integer between given two numbers.

The function takes upper and lower bounds as arguments.

Pass lower bound as the first argument and upper bound as the second argument to the function.

```
import random
# randint function
num = random.randint(5, 15)
print(f"Random number between 5 and 15 is {num}")
num = random.randint(50, 100)
print(f"Random number between 50 and 100 is {num}")
```

Output: (may vary)

Random number between 5 and 15 is 12 Random number between 50 and 100 is 79

## 3. uniform() Function

The uniform() function also generates a random number between given 2 points but it is a floating point number.

The function takes 2 numbers as upper and lower bound of range arguments.

```
import random
# uniform function
num = random.uniform(5, 15)
print(f"Random number between 5 and 15 is {num}")
num = random.uniform(50, 100)
print(f"Random number between 50 and 100 is {num}")
```

Output: (may vary)

Random number between 5 and 15 is 12.135485280124156 Random number between 50 and 100 is 87.54564757637775

## 4. randrange() Function

The randrange() function generates a random number between given 2 points with a given step.

It has 3 arguments:

- start: lower bound of range
- stop: upper bound of range
- step: step size

Suppose we want numbers between 1 and 10 with a step size of 2, then only 1, 3, 5, 7, and 9 will be generated.

```
import random
# randrange function
num = random.randrange(1, 10, 2)
print(f"Random number between 1 and 10 with step size of 2 is {num}")
num = random.randrange(1, 10, 3)
print(f"Random number between 1 and 10 with step size of 3 is {num}")
```

Output: (may vary)

Random number between 1 and 10 with step size of 2 is 5 Random number between 1 and 10 with step size of 3 is 4

## 5. choice() Function

The choice() function randomly chooses an element from a sequence.

The function takes a list, tuple, string, etc. as an argument.

```
import random
# choice function
# Choose random element from list
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = random.choice(list)
print(f"One choice from the list is {num}")
str = "Hello World"
chr = random.choice(str)
print(f"One choice from the string is {chr}")
```

Output: (may vary)

One choice from the list is 5 One choice from the string is H

## 6. sample() Function

The sample() function randomly chooses k unique elements from a sequence.

The function takes a list, tuple, string, etc. as the first argument and a number of elements to choose as the second argument.

```
import random
# sample function
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = random.sample(list, 3)
print(f"Three choices from the list are {num}")
str = "Hello World"
chr = random.sample(str, 5)
print(f"Five choices from the string are {chr}")
```

Output: (may vary)

Three choices from the list are [1, 2, 3] Five choices from the string are ['e', 'l', 'o', ' ', 'W']

## 7. shuffle() Function

The shuffle() function randomly shuffles the elements of a sequence and changes the order of elements.

```
import random
# shuffle function
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(list)
print(f"Shuffled list is {list}")
```

Output: (may vary)

Shuffled list is [10, 5, 1, 2, 4, 3, 9, 7, 8, 6]

## Custom Function To Generate Random Number

We mentioned earlier that if you have a random number between 0 and 1 then you can generate a random number between any given range.

Let's create our own function to generate random numbers in a range.

For this, we will use random.random() function to generate a random number between 0 and 1. And then we will expand this random number to a range.

Let's see the program first then we will see how the function works.

```
import random
# custom function for random number in range
def random_number_in_range(min, max):
# number between 0 and 1
r = random.random()
# expand the number to a range
num = r * (max - min) + min
return num
# number between 0 and 10
num = random_number_in_range(0, 10)
print(num)
# number between 10 and 20
num = random_number_in_range(10, 20)
print(num)
```

Output: (may vary)

6.328116209039756 15.00905517135427

## Conclusion

The random module is used for any random task, whether it is to generate a random number, select a random element from a list, shuffle a list, etc.

Here we have seen how to generate a random number in Python with multiple useful examples.

Reference: random module