Random Module in Python

Generating random numbers is required for different reasons. For example, creating a random number as a user’s password or pin code etc.

Python makes it quite easy creating the random numbers whether it is the matter of creating a random number within a range, integer numbers as well as generating floating random numbers.

The built-in module random has a number of functions to fulfill that purpose. A few of these functions include:

  • Randrange
  • Randint
  • Random
  • Shuffle
  • Choice

Few of these functions are for generating the integer random number while other generate float numbers. The next section shows you examples of these functions.

Generating a random integer number by Randrange

The randrange function returns a random number for the given range. This function can be used in two ways. The syntax of first way is:

random.randrange(stop)

In that case, the maximum number is provided and a random number is generated between 0 to that number (inclusive).

The example below generates a random number between 0 and 500 by using randrange function:

# Generate Random Numbers - RandRange demo

import random



rand_num = random.randrange(500)

print (rand_num)

The other way of using this function is providing the start, stop and step arguments as follows:

random.randrange(start, stop[, step])

The start and stop numbers are inclusive.

The following example shows generating a random number between 10 to 1000 that must be even.

The code:

# Generate Even Random Numbers in range

import random



rand_even = random.randrange(10, 1000, 2)

print (rand_even)

Similarly, a random odd number between 1 to 500 can be created as follows:

# Generate Odd Random Numbers in range

import random


rand_odd = random.randrange(1, 500, 2)

print (rand_odd)

The example of using randint number

As the name shows, this random module’s function is used to generate an int pseudo-random number. The syntax for using the randint function is:

random.randint(a, b)

The arguments a and b must be integer values. It returns ValueError if floating point numbers are given as argument.

The examples below show generating positive, negative and a random int number either positive or negative between two values.

Generating positive int random number

The example below generates an int number between 5 and 50:

The code:

# Generate int random number: randint

import random


print("The Int Random Number: % s" % (random.randint(5, 50)))

Example of a negative random number:

In this example, a negative number is generated:

# Generate negative int random number demo

import random



print("The negative integer number: % s" % (random.randint(-55, -5)))

Note that the first number must be smaller than the second; otherwise, a ValueError is generated.

Generating either negative or positive number example

#Negative or positive random number

import random



print("The Random Numer (+ve or -ve): % s" % (random.randint(-15, 15)))

Generating pseudo-random floating point number

In this section, we will have a look at the functions for generating the floating numbers. The first function is random that returns the next random floating point number.

This random function does not take any argument and used as follows:

random.random()

If used that way, it returns a random floating number in the range of 0.0 and 1.0.

The example below shows displaying a number:

#Floating number demo

import random



flt_ran = random.random()

print("Floating Point Random Number [0.0 , 1.0]: ", flt_ran )

Generating random floating point number between 0.0 and 5.0

import random



flt_ran_five = random.random()*5

print("Floating Point Random Number [0.0 , 5.0]: ", flt_ran_five )

Python random

An example of using uniform function

The uniform function of the random module also returns the floating point random number. The difference is, you may specify a range, unlike the random function that returns a random number between (0.0 – 1.0).

Also, the value b may be included in the range depending on the floating-point rounding in the equation. In the case of the random function, the 1.0 is not included.

The example below shows generating the random floating point number between (10.5, 20.5):

#Floating number demo by uniform function

import random



flt_rand_uni = random.uniform(10.5, 20.5)

print("Floating Point Random Number in Range (10.5, 20.5): ", flt_rand_uni )

The choice function

The choice function returns a random element from a sequence like a list, tuple etc. The usage can be, for example, a lucky draw. There, you have a list of given numbers or codes that are part of the draw. The choice function can be used to pick a number randomly.

The syntax for using the choice function is:

random.choice(seq)

In the example below, we have a list of ten numbers. The choice method is used to return the random element and displayed on the screen.

#Choice function demo

import random



draw_lst = [45747, 45624, 45745, 748458, 254231, 4145474, 1458745, 584757, 589654, 968457]



print("The Winning Number is: ", random.choice(draw_lst))

The result as I executed this code:

The Winning Number is:  45745

How to use Shuffle function

The shuffle method randomizes the elements of the sequence in place. For example, if we have a list:

[1,2,3,4,5]

The shuffle function of the random module can randomize the order of elements. An example below shows a list and its output before and after using the shuffle function:

#Shuffle function demo

import random



shuff_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



#List Before

print("List Before", shuff_lst)



#List after shuffling

random.shuffle(shuff_lst)

print("List After Shuffling", shuff_lst)

The output as I executed this code:

List Before [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List After Shuffling [7, 4, 9, 2, 5, 1, 6, 3, 8, 10]

Every time you run this code, it should produce a different result after using the shuffle function.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!