Generate Random Numbers in Python



Python includes a built-in package i.e random module for generating random numbers. In this article, we will show you how to generate random numbers in python using different methods -

Using random.seed() Method

The random number generator is initialized using the seed() method. To generate a random number, the random number generator requires a starting numeric value (a seed value).

The random number generator utilizes the current system time by default. To change the random number generator's starting number, use the seed() method. You will get the same random number if you use the same seed value twice. The following is the syntax for the random.seed() function -

random.seed(x, version)

Where,

  • x(optional): The seed value required to generate a random number. If it is an integer, it is utilized directly; otherwise, it must be converted to an integer.
  • version: An integer that specifies how to convert the 'x' argument to an integer. The default value is 2. The generator uses the current system time if the value is None.

Example

The following program returns a random element from the list using the random.random() and seed() methods -

import random
# Setting the seed value to 5
random.seed(5)
print(random.random())

# Setting the same seed value as above i.e 5
random.seed(5)
print(random.random())

The program gives this output when it is run -

0.62290169489
0.62290169489

We set the seed value to 5 and then used the random() function of the random module to generate a random value. Then we set the seed value to 5 again and used the random() function to generate the same random value as before. This is how the seed() function is used.

Using random.randrange() Method

The randrange() method selects an element at random from the specified range and returns it. The following is the syntax for the random.randrange() function -

random.randrange(start, stop, step)

Where,

  • start(optional) is an integer indicating the starting position, and 0 is the default.
  • stop(required) is an integer indicating the end position.
  • step(optional) is an integer indicating the incrementation. 1 by default.

Example

The following program returns a random number between the given range using the randrange() function -

import random

# generating a random number between 10(included) and 20(not included)
print("Random Number Generated = ", random.randrange(10, 20))

You will see this result after executing the program -

Random Number Generated = 13

The randrange() function is used here to generate a random number from a range. We passed the starting value (lower limit) and ending value (upper limit) as arguments, and it generated a random number between those ranges

Using random.randint() Method

The randint() method returns an integer value representing a randomly chosen element from the given range. This is an alias for randrange(start, stop+1). The following is the syntax for the random.randint() function -

random.randint(start, stop)

Where,

  • start(required): an integer indicating the starting position.
  • stop(required): an integer indicating the end position.

Example

The following program returns a random number between the given range using the randint() function -

import random
# generating a random number between 10 and 20(both 10 and 20 numbers included)
print("Random Number Generated = ", random.randint(10, 20))

Executing the above program will generate the following output -

Random Number Generated = 20  

The difference between randrange() and randint is that randint includes the upper limit in the range, whereas randrange() excludes the upper limit. We can add a step value to the randrange() function, but not to the randint() function.

Using random.random() Method

The random() method generates a random floating-point value between 0 and 1. The following is the syntax for the random.random() function -

random.random()

Example

The following program returns a random number between the given range using the random() function -

import random
# generating a random floating-point number between 0 and 1
print("Random Number Generated = ", random.random())

This output will be displayed when the program runs -

Random Number Generated = 0.15685132230226662

Using random.choice() Method

The choices() method returns a list containing the element from the provided sequence that was chosen at random. The sequence could be a string, a range, a list, a tuple, or anything else.

Example

The following program returns a random number from the list -

import random

#Input list
given_List = [ 1, 6, 3, 9, 10, 24, 475, 483, 2656]
print('The first Random Element from the list:',random.choice(given_List))
print('The Second Random Element from the list:',random.choice(given_List))

After running the program, you will get this result -

The first Random Element from the list: 6
The Second Random Element from the list: 24

Using random.uniform() Method

The uniform() method generates a random floating number between the two input values (both numbers included). Below is the syntax for the random.uniform() function -

random.uniform(x, y)

Where -

  • X: is a number representing the lowest possible outcome.
  • Y: is a number representing the highest possible outcome.

Example

The following program returns a random floating number between the given range using the uniform() function -

import random
# generating a random number between 10 and 20(both 10 and 20 are also included)
print("Random Number Generated = ", random.uniform(10, 20))

When you run the program, it will show this output ?

Random Number Generated = 12.845596876772472

In this case, the uniform() function is used to generate a random floating number from a range. We gave it the starting (lower limit) and ending (upper limit) values as arguments, and it generated a random float/decimal number between those two ranges.

Updated on: 2025-04-23T17:07:49+05:30

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements