Python Examples

Python Program to Generate a Random Number

Python random number generation module is applicable in many functions like lotteries, giveaways, and other games. To generate random number in Python, randint() function is used. Python has defined a set of functions under the random module to generate random numbers.
python random number

Source

Python random number

 To generate random numbers using the for loop, follow the program given below:

                    

import random

randomlist = []

for a in range(0,4):

q = random.randint(1,30)

randomlist.append(q)

print(randomlist)

Output:

[10, 5, 21, 1]

Random Number Generator Python

There are many modules under the random module used to generate random numbers. These include:

1. choice()- This function generates any random number from a list, tuple, or string.

2.randrange(beg, end, step): This function generates random numbers from a specified range and also allows steps to be included. 

3. random(): This function is used to generate a random float number that is greater than or equal to 0 and less than 1. 

4. shuffle(): This function is used to shuffle the position of the elements in a list.

5. uniform(a,b)– this function generates random float integers between 2 numbers. 

What is random Randint in Python?

The random Randint() in Python is used to generate random numbers in Python. We need to provide a range for the Python program Once the range is defined, a random number is then generated.

Syntax:

random.randint(a,b)

This function returns a random number between a and b. Where n>a and n<b. 

For example:

                    

import random


print(random.randint(0,9))


Output

7

How do you pick a random number between 1 and 10 in Python?

To generate random numbers between 1 to 10 we can use the randint() method:

  • This function returns any random number between 1 and 10. 

                    

from random import randrange

print(randrange(11))

  • The users can also return any random number between min, max range. 

                    

import random

print(random.randint(0,9))

  • Users can also change the parameters of the function to generate a number between 1 to 10. 

                    

import random

x = random.randint(1,10)

print(x)

How do you Generate a Random Number in a Range in Python?

In Python, if the user mentions the upper and the lower limits of a given range, a random number will be generated from that range. The random numbers will be stored in a list. 

Example: 

Input : num = 10, start = 40, end = 60

Output : [43, 60, 50, 53, 67, 46, 57, 47, 58, 48]

The output contains ten random numbers in the range [40, 60].

Input : num = 6, start = 11, end = 15

Output : [15, 11, 13, 15, 12, 11]

The output contains five random numbers in the range [11, 15].

To generate random numbers, Python has an inbuilt random module. Using this module and the random function, random numbers are generated. 

Source Code:

                    

def Rand(start, end, num):

    res = []

    for j in range(num):

        res.append(random.randint(start, end))

    return res

# Driver Code

num = 10

start = 20

end = 40

print(Rand(start, end, num))

Output:

[23, 27, 28, 38, 20, 36, 37, 30, 33, 30]

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.