Hit enter after type your search item

# The 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:

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:

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

### 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:

Example of a negative random number:

In this example, a negative number is generated:

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

Generating either negative or positive number example

### 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:

Generating random floating point number between 0.0 and 5.0 ### 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):

### 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.

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:

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.

This div height required for enabling the sticky sidebar