Generate random numbers in Python
In this post, you will learn different ways to find a random number in the Python programming language.
Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful in improving your logical programming skills and is helpful in game development and other logical application development.
There are different ways to generate random numbers in Python. Here, we have mentioned most of them.
Python Random Module
There is an in-built random module in Python that's used to generate pseudo-random variables. We can use this to perform some activities randomly, for example, getting a random number, choosing an irregular element from a list, randomly shuffle elements, and so on. This module provides several functions to perform operations randomly. Here, you will know how to select an element randomly.
The random.choice() method of the Python random module returns a random element from the given sequence.
Syntax of random.choice()
random.choice(sequence)
Here, the sequence can be any list, string, tuple or any other kind of sequence. It returns a single item from the sequence. It is a required parameter. If we pass an empty list to the random.choice() function, it will raise an error IndexError.
Here, we have mentioned few examples of random.choice() method.
import random
x = "ETUTORIALSPOINT"
print("Random from x is: ",random.choice(x))
y = "LAPTOP"
print("Random item y is: ",random.choice(y))
Output of the above code:
Random from x is: T
Random item y is: O
Python randint()
The randint() is an in-built function of the random module in Python3. It returns an integer number selected element from the specified range.
Syntax of randint()randint(start, end)
Here, both start and end must be integer type values.
Random number between 5 and 20 is 12
Random number between 10 and 30 is 15
Python randrange()
The randrange() method of Python returns a randomly selected element from the specified range.
Syntax of randrange()random.randrange(start, stop, step)
import random
print ("Random number from 2-13 : ",end="")
print(random.randrange(2, 13))
Output of the above code:
Random number from 2-13 : 8
Python random()
The random() method of Python returns a random floating number between 0 and 1.
import random
print(random.random())
Output of the above code:
Random number :
0.13713447068834828
Python uniform()
The uniform() method of Python returns a random floating number between the two specified numbers.
import random
a = 12
b = 45
# printing the random number
print("The random number between 12 and 45 : ", end ="")
print(random.uniform(a, b))
Output of the above code:
The random number between 12 and 45 : 40.598655031953655
Python sample()
The sample() method returns a list with a randomly selection of a specified number of items from the given sequence.
Syntaxrandom.sample(sequence, k)
Here, both sequence and k are the required parameters. The sequence can be any sequence, list, set, range etc and the k is the size of the returned list.
import random
seq = (55, 22, 52, 22, 13, 51, 34, 38, 19)
random = random.sample(seq, 5)
print(random)
Output of the above code:
[55, 13, 19, 22, 34]
Python shuffle()
The shuffle() is used to shuffle a sequence (list). Shuffling means changing the position of the elements of the sequence.
Syntaxrandom.shuffle(sequence, function)
Here, the sequence is the required parameter. The function returns a number between 0.0 and 1.0.
import random
def shuffleFunc():
return 0.1
mylist = ["cat", "dog", "cow", "goat", "hen"]
random.shuffle(mylist, shuffleFunc)
print(mylist)
Output of the above code:
['dog', 'cow', 'goat', 'hen', 'cat']
Python seed()
The seed() method is used to initialise the random number generator. The random number generator needs a number (seed value) to start with, to be able to generate a random number.
Syntaxrandom.seed(a, version)
Here, a is the seed value needed to generate a random number, and version is an integer specifying how to convert the a parameter into an integer.
import random
random.seed(4)
for i in range(4):
print(random.random(), end = ' ')
print('\n')
Output of the above code:
0.23604808973743452 0.1031660342307158 0.396058242610681 0.15497227080241027
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary