Related Articles

# Python Random Module

• Last Updated : 22 Jul, 2021

Python Random module is an in-built module of Python which is used to generate random numbers. These are pseudo-random numbers means these are not truly random. This module can be used to perform random actions such as generating random numbers, print random a value for a list or string, etc.

Example: Printing a random value from a list

## Python3

 `# import random``import` `random`` ` `# prints a random value from the list``list1 ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``]``print``(random.choice(list1))`

Output:

`2`

As stated above random module creates pseudo-random numbers. Random numbers depend on the seeding value. For example, if the seeding value is 5 then the output of the below program will always be the same.

Example: Creating random numbers with seeding value

## Python3

 `import` `random`` ` `random.seed(``5``)`` ` `print``(random.random())``print``(random.random())`

Output:

```0.6229016948897019
0.7417869892607294```

The output of the above code will always be the same. Therefore, it must not be used for encryption.

Let’s discuss some common operations performed by this module.

## Creating Random Integers

random.randint() method is used to generate random integers between the given range.

Syntax :

`randint(start, end)`

Example: Creating random integers

## Python3

 `# Python3 program explaining work``# of randint() function`` ` `# import random module``import` `random`` ` `# Generates a random number between``# a given positive range``r1 ``=` `random.randint(``5``, ``15``)``print``(``"Random number between 5 and 15 is % s"` `%` `(r1))`` ` `# Generates a random number between``# two given negative range``r2 ``=` `random.randint(``-``10``, ``-``2``)``print``(``"Random number between -10 and -2 is % d"` `%` `(r2))`

Output:

```Random number between 5 and 15 is 7
Random number between -10 and -2 is -9```

## Creating Random Floats

random.random() method is used to generate random integers between 0.0 to 0.1.

Syntax:

`random.random()`

Example:

## Python3

 `# Python3 program to demonstrate``# the use of random() function .``     ` `# import random``from` `random ``import` `random``     ` `# Prints random item``print``(random())`

Output:

`0.3717933555623072`

## Selecting Random Elements

random.choice() function is used to return a random item from a list, tuple, or string.

Syntax:

`random.choice(sequence)`

Example: Selecting random elements from the list, string, and tuple

## Python3

 `# Python3 program to demonstrate the use of``# choice() method`` ` `# import random``import` `random`` ` `# prints a random value from the list``list1 ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``]``print``(random.choice(list1))`` ` `# prints a random item from the string``string ``=` `"geeks"``print``(random.choice(string))`` ` `# prints a random item from the tuple``tuple1 ``=` `(``1``, ``2``, ``3``, ``4``, ``5``)``print``(random.choice(tuple1))`

Output:

```2
k
5```

## Shuffling List

random.shuffle() method is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence. Here, the shuffling operation is inplace.

Syntax:

`random.shuffle(sequence, function)`

Example: Shuffling a List

## Python3

 `# import the random module``import` `random`` ` ` ` `# declare a list``sample_list ``=` `[``1``, ``2``, ``3``, ``4``, ``5``]`` ` `print``(``"Original list : "``)``print``(sample_list)`` ` `# first shuffle``random.shuffle(sample_list)``print``(``"\nAfter the first shuffle : "``)``print``(sample_list)`` ` `# second shuffle``random.shuffle(sample_list)``print``(``"\nAfter the second shuffle : "``)``print``(sample_list)`

Output:

```Original list :
[1, 2, 3, 4, 5]

After the first shuffle :
[4, 3, 5, 2, 1]

After the second shuffle :
[1, 3, 4, 5, 2]```

## List of all the functions in Random Module

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up