In this article, we have provided a python code that is capable of generating random numbers.
Prerequisite to Python Program to Generate a Random Number
- Python Input, Output
- python modules
To generate random numbers in using the python program we require the random module to import in our program.
Python Program to Generate a Random Number
import random print(random.randint(0,9))
If you run this program in your system then you might get a different output. The randint() is a method of random module which is capable of generating random integer values.
Random Module in Python
Python has built-in module random which has some set of methods that can be used to generate and manipulate random numbers.
Choice method is used to pick a random number form a given container such as list, array, tuples, etc.
import random lis = [132,142,152,249] print(random.choice(lis))
2. randrange(start, end, steps):
This method of random can be used to generate a random number but within a range from start to end.
import random print(random.randrange(1,200,5))
This method is used to generate a float type random number which is less than 1 and greater than or equal to 0
import random print(random.random())
4. shuffle() :
This method is capable of shuffling the list elements and arrange them randomly.
import random lis = [1,2,3,4,5,6,7,8,9,10] random.shuffle(lis) print(lis)
[8, 6, 5, 2, 9, 10, 3, 4, 7, 1]
People are also reading:
- Python Program To Display Powers of 2 Using Anonymous Function
- WAP to print the 1 to 10 Multiples of a Number
- Python Program to Convert Decimal to Binary, Octal and Hexadecimal
- WAP to convert a given number of days into years, weeks and days
- Python Program to Find the Factors of Number
- WAP to Display Fibonacci Series
- Programming in C and Java to convert from octal to decimal
- WAP to check whether the entered number is prime or not
- Python Program to Display the Multiplication Table
- WAP to find the divisors of a positive Integer