# How to Make a Random Number Generator in Python

How to Make a Random Number Generator in Python | A random number is a pseudo number, random number generation is a process of generating a random number, this cannot be predicted and a random number contains some kind of pattern.

We will see these below Python program examples:–

1. How to make a random number generator in python
2. How can we generate random numbers in python using methods
3. How to generate random numbers in python without using random
4. How to generate a random 4 digit number in python
5. Python random number without repeating
6. Python random number between 0 and 1
7. Python generate random number in range
8. Generate 20 random numbers between 1 and 100 python

## How to Make a Random Number Generator in Python

To generate a random first we import a random class available in the python library, this helps to generate some random integer, and then we use randint() and give a range that generates a random number between them. Every time we run the program it gives a different integer value hence we call it random.

``````import random
print(random.randint(0, 11))``````

Output:

1

## How can We Generate Random Numbers in Python Using Methods

Now by using random(), choice(), and seed() methods we generate random numbers. The random() method is used to generate float variables less than or equal to 1 or greater than 0. While the seed() method saves the state of the random function so that it generates some random number on multiple executions. The choice() function returns a random number a list, tuple, or a string.

``````import random

l = [11, 12, 51, 14, 58, 64]
print(random.choice(l))

print("A random number between 0 and 1 is: ", end="")
print(random.random())

random.seed(5)

print("The mapped random number with 5 is: ", end="")
print(random.random())

random.seed(7)

print("The mapped random number with 7 is: ", end="")
print(random.random())
``````

Output:

12
A random number between 0 and 1 is: 0.3948234964231735
The mapped random number with 5 is: 0.6229016948897019
The mapped random number with 7 is: 0.32383276483316237

## How to Generate Random Numbers in Python Without Using Random

To generate random numbers without using random we use time. Time is a built-in module in python which has several functions. In the code, first, we have imported time and then have defined a function rand_num() which takes two parameters minimum and maximum, the rand_num() generates the random number between this minimum and maximum.

``````import time

def rand_num(minimum,maximum):
now = str(time.perf_counter())
rnd = float(now[::-1][:3:])/1000
print(minimum + rnd*(maximum-minimum))

min = 1
max= 19
rand_num(min, max)
``````

Output:

11.764

## How to Generate a Random 4 Digit Number in Python

We try to generate a 4 digit random number in python which can also work like OTP.

Step1: First, we import math and random modules available in python.
Step2: Then we define a function, we give a number from which it generates 4 digits random number.
Step3: Initialize some variable to an empty string which stores the resultant random number.
Step4: Use for loop and specify the range for 4 then iterate over the digits. Print the output.

``````import random
import math
def rand_num():
num = "145689876543"
four_digits = ""
for i in range(4):
four_digits = four_digits+ num[math.floor(random.random()*10)]
print(four_digits)

rand_num()
``````

Output:

4819

## Python Random Number Without Repeating

To avoid repetition of numbers, we can use random.choices() method available in the random module. In the below code, we print the random numbers in the list of ranges 1 to 100 without repeating any numbers.

``````import random
L = [i for i in range(1, 100)]
List = random.choices(L, k = 9)
print(List)
``````

Output:

[7, 9, 43, 82, 13, 23, 63, 94, 58]

## Python Random Number Between 0 and 1

To find the random between 0 and 1 we can use the random() function available in the random module in python which generates a random number less than or equal to 1 and greater than or equal to 0.

``````import random
print(random.random())
``````

Output:

0.04958931338977146

## Python Generate Random Number In Range

To generate a random number in a specified range we have a predefined function in the python library called randint() which takes two parameters starting range and ending range and generates the random number between these two ranges including the start and end specified.

``````import random
print(random.randint(1, 9))
``````

Output:

4

## Generate 20 Random Numbers Between 1 And 100 Python

We can generate 20 random numbers by random.sample() method available in python, it takes a range and an integer value as parameters. Range specifies the range to generate a random number and integer value says how many random numbers to generate. This gives the output in the form of a list.

``````import random
l = random.sample(range(1, 100),20)
print(l)
``````

Output:

[18, 38, 54, 19, 70, 16, 74, 40, 72, 88, 24, 14, 75, 82, 25, 48, 13, 71, 92, 9]

Also See:- How to Swap Elements in List of Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!