# Python Random Number Generator with Examples

## How to Generate Random Numbers in Python

In this tutorial, you'll learn how to generate a random number in Python. You'll learn how to generate a random number using different methods with examples.

To generate a random number in Python, you can use either the randint or randrange function.

Note that you need to import random module to use randint and randrange.

### Generate a Random Number using randint()

The syntax of the randint() function is as follows:

randint(startstop)

#### Arguments

• start: A starting integer number to generate. It defaults to 0 if not specified
• stop: An integer number that randint() function generates up to but not including this number.

### Generate a Random Number using randrange()

The syntax of the randrange() function is as follows:

random.randrange(stop)

or

random.randrange(startstop[, step])

#### Arguments

• start: A starting integer number to generate. It defaults to 0 if not specified
• stop: An integer number that randrange() function generates up to, but not including this number.
• step: The step argument is a difference between each number in the sequence. It defaults to 1, if not specified.

### Examples

#### Generate a Random Number using randint()

##### Example 1: Generate a Random Number between 0 and 1

The following code illustrates an example of generating a random number between 0 and 1:

import random
#random.randint(start, stop)
#generate a random number between 0 and 1
random_number = random.randint(01)
print(random_number)
##### Example 2: Generate a Random Number between 1 and 99

The following sample Python program illustrates an example of generating a random number between 1 and 99:

import random
#random.randint(start, stop)
#generate a random number between 1 and 99
random_number = random.randint(199)
print (random_number)

#### Generate a Random Number using randrange()

##### Example 3 : Generate a Random Number between 0 and 1

The following code illustrates an example of generating a random number between 0 and 1 using randrange():

import random
#random.randrange(stop)
#random.randrange(start, stop[, step])
#generate a random number between 0 and 1
random_number = random.randrange(2)
print (random_number)
#or
random_number = random.randrange(02)
print (random_number)
##### Example 4: Generate a Random Number between 1 and 99

The following code illustrates an example of generating a random number between 1 and 99 using randrange():

import random
#random.randrange(start, stop[, step])
#generate a random number between 1 and 99
random_number = random.randrange(1100)
print (random_number)
##### Example 5: Generate a Random List of Numbers

The following program generates a random list of numbers between 1 and 100:

import random
#random.randint(start, stop)
#generate a random list of numbers between 1 and 99
random_list_of_numbers = [random.randint(1100for iter in range(10)]
print (random_list_of_numbers)

Here, the above program generates a random list of numbers containing 10 items.

##### Example 6: Generate a Random List of Numbers without Repeating

The following code illustrates how to generate a random list of numbers without duplicates:

import random
#random.randint(start, stop)
#generate a random list of numbers between 1 and 99
random_list_of_numbers_no_repeating = []
for i in range(10):
random_number = random.randint(1,100)
if(random_number not in random_list_of_numbers_no_repeating):
random_list_of_numbers_no_repeating.append(random_number)
print (random_list_of_numbers_no_repeating)

In this tutorial, you've learned how to generate a random number in Python. You can generate a random number in many ways. This tutorial described how to use both randint() and randrange().