random-module-in-python

Introduction

The random module is a built-in module in Python that generates pseudo-random variables. It can be used to generate random numbers, choose a single option from multiple options, shuffle elements randomly, and so on.

At some point in time, we may need to generate some random data. This is that time when the random module in python comes into action.

In this tutorial, we’ll look at some of the most used methods in random modules and see some examples corresponding to those methods.

Some of the methods in the Random module

Method Description
seed() initialize the random number generator
random() returns random floating-point numbers between 0.1 to 1.0
randint() returns a random number between two numbers
randrange() returns a random number between a and b
choice() returns random elements from the sequence
choices() returns a list selected randomly from a sequence
shuffle() returns a randomly shuffled sequence
uniform() returns random floating point number between two given numbers
sample() returns a sequence of length k
getstate() returns the current internal state of the random number generator
setstate() returns the internal state of the random number generator

 

Some of the examples of the random methods

random.random()

The random() method generates a random floating-point number ranging from 0.0 to 1.0

import random

print("First try: ", random.random())
print()
print("Second try: ", random.random())

Output

First try:  0.494284214446681

Second try:  0.7626502074368717

Here we can see that in both cases the random() method generates two float numbers ranging from 0.0 and 1.0

 

random.randint()

The randint() method takes two parameters. These two parameters are the upper and lower boundaries of the number that you want to generate.

randint() method generates an integer number between two boundaries including two boundaries too.

import random

print("First try: ", random.randint(1,3))
print()
print("Second try: ", random.randint(1,3))

Output

First try:  2

Second try:  3

As we can see that this method generates a random integer number between 1 and 3 including both 1 and 3.

 

random.randrange()

The randrange() method returns a randomly selected number from a given specified range. You can also provide step parameters to randrange() method. The default step is 1.

import random

print("First try: ", random.randrange(1,10))
print()
print("Second try: ", random.randrange(1,10, step=2))

Output

First try:  1

Second try:  9

 

random.uniform()

The uniform() method returns a randomly selected float number within a given specific range.

import random

print("First try: ", random.uniform(1,10))
print()
print("Second try: ", random.uniform(1,10))

Output

First try:  9.768400900579408

Second try:  7.728800809249086

 

random.choice()

The choice() method returns a random value from a given list.

import random
choice = ['rock', 'paper', 'scissor']

print("First try: ", random.choice(choice))
print()
print("Second try: ", random.choice(choice))

Output

First try:  paper

Second try:  scissor

 

random.choices()

The choices() method returns the list of randomly selected values from the given list.

import random
choice = ['rock', 'paper', 'scissor']

print("First try: ", random.choices(choice, k=7))
print()
print("Second try: ", random.choices(choice, k=5))

Output

First try:  ['scissor', 'scissor', 'paper', 'scissor', 'rock', 'rock', 'rock']

Second try:  ['scissor', 'scissor', 'scissor', 'scissor', 'paper']

Here, you can pass the ‘k’ parameter to tell the choices() method how many random values you want to pick. As we can see in the above example first we pass k = 7 and got seven random values and the second time we pass k=5 and got five random values.

 

random.shuffle()

The shuffle() method shuffles the values inside the list and returns that list.

import random
choice = ['rock', 'paper', 'scissor']

random.shuffle(choice)
print("After first try: ", choice)

print()

random.shuffle(choice)
print("After second try: ", choice)

Output

After first try:  ['paper', 'scissor', 'rock']

After second try:  ['rock', 'scissor', 'paper']

 

random.sample()

The sample() method returns a list that is selected from a given sequence. You should pass ‘k’ which is a sample that determines the length of the sample.

import random
choice = [1,2,3,4,5,6,7]

print("First try: ",random.sample(choice, k=4))

print()

print("Second try: ", random.sample(choice, k=6))

Output

First try:  [7, 4, 2, 1]

Second try:  [4, 5, 1, 2, 7, 6]

 

Conclusion

You can use the random module to generate some random integer or float, shuffle the value, select a random value from a sequence, and so on. You can use the random module in cryptography, to generate a random password, and so on.

 

Reference

For more information check out python documentation of random module

Happy Learning 🙂

Leave a Reply

Your email address will not be published.