Beginners or python experts can learn complete knowledge on how to generate a random number in python from this tutorial. Having knowledge of these Python programming topics like Python Input, Output, and Import, Python Random Module is the best way to understand the concept of Python Random Numbers Generation. If you don’t know the coding part of generating random numbers in python, then you’re not more called a python coder.
This tutorial of Python Program to Generate Random Numbers include the following modules:
- Random Numbers in Python
- Syntax for Generating Random Number
- Generating a Single Random Number
- How to Generate a Random Number in Python Example
- Python Program to Generate a Random Number
- Generating Number in a Range
- Generating a List of Numbers Using For Loop
- Generate Random Numbers in Python Using random.sample()
- Random Number Operations
Defining a set of functions in python which are utilized to generate or manipulate random numbers via the random module. The random module functions completely rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0.
In various things such as games, lotteries, or any application requiring a random number generation, we can use these particular types of functions. Look at the syntax of the random numbers below and then go for a detailed explanation on how to generate random numbers in python.
Initially, we have to import the random module and then apply the following syntax:
import random random.randint(a,b)
Read More: How to Use Pythons Len Method
Check out the code snippet below to see how the random() method works to generate a float number between 0 and 1.
import random n = random.random() print(n)
What if, however, you wanted to select a random integer that was between 1 and 100 but also a multiple of five? This is a little more complicated. The process is the same, but you’ll need to use a little more arithmetic to make sure that the random integer is in fact a multiple of five. Check out the code below:
import random for x in range(10): print random.randint(1,21)*5, print
Basically, this code will generate a random number between 1 and 20, and then multiply that number by 5. So not only will every number printed be a multiple of 5, but the highest number that can be printed is 100 (20*5=100). Play around with the code yourself and see if you can generate a random number between 1-100 where every number generated is a multiple of 10!
# Program to generate a random number between 0 and 9 # importing the random module import random print(random.randint(0,9))
To generate random numbers in python, the randint() method helps to generate an integer between a given range of numbers.
import random n = random.randint(0,22) print(n)
Also, there is an opportunity to generate a list of numbers using for loop. Initially, we create an empty list and then add the random numbers generated to the empty list one after the other. Check the below code snippet to understand how it works:
import random randomlist =  for i in range(0,5): n = random.randint(1,30) randomlist.append(n) print(randomlist)
[10, 5, 21, 1, 17]
However, you can also make use of the available sample() method in a random module to immediately generate a list of random numbers. In the below code, we define a range and give how many random numbers we need to generate.
import random #Generate 5 random numbers between 10 and 30 randomlist = random.sample(range(10, 30), 5) print(randomlist)
[16, 19, 13, 18, 15]
- randrange(beg, end, step)
- uniform(a, b)
The program for using all of these functions to generate random numbers in Python is as follows:
#importing the random library import random # using the choice() function print(‘Generate random number in python using the choice() function: ‘) print(random.choice([1,2,3,4,5,6])) print(‘\r’) # using the random() function print(‘Generate random number in python using the choice() function: ‘) print(random.random()) print(‘\r’) # using the shuffle() function # Initializing the list li = [7, 4, 12, 3, 17] # Printing list before shuffling print (‘The list that is given before shuffling is: ‘, end=’’) for i in range(0, len(li)): print (li[i], end=’’ ‘’) print(‘\r’) random.shuffle(li) # Printing the sample list [7, 4, 12, 3, 17] after shuffling print(‘Generate random number in python using the shuffle() function: ‘) print (‘The list after shuffling using the shuffle() function is : ‘, end=’’) for i in range(0, len(li)): print (li[i], end=’ ‘) print(‘\r’) #using the randint() function print(‘Generate random number in python using the randint() function: ‘) print(random.randint(1,10)) print(‘\r’) #using the randrange() function print("A random number from range is : ", end="") print(random.randrange(20, 50, 3))