How to generate a random number in python :
In this tutorial, we will learn how to create a random number in python.Generating a random number in python is easier than you think.Everything is already defined in a module called random. Just import it and use its inbuilt functions.
All the code in this tutorial is compatible with python 3.If you have python 2 installed on your system, maybe you will have to change a few lines 🙂
Example 1 : Print a random float :
import random print(random.random())
It will print one random float value between 0 to 1. 0 <= value < 1 .If you run this code, it will print some values like 0.155304863803.
Example 2 : Print a random float in range :
import random print(random.uniform(1,10))
It will print one random number within range from 1 to 10. The value of 10 may or maynot include the second value. The output will something like 8.6874876844.
Example 3 : Print a random integer :
import random print(random.randrange(10))
It will print an integer from 0 to 9. Run it and you will always get one value in this range.
Example 4 : Print an integer number within a range :
To print an integer in a given range, we have two functions : randrange(a,b) and randint(a,b). The only difference between both is that randint(a,b) will include all integers from a to b,but randrange(a,b) will include integers from a to b-1.That means randint(a,b) is similar to randrange(a,b+1).Let’s have a look how to implement both :
import random print("Random number using randrange : "); print(random.randrange(2,10)) print("Random number using randint : "); print(random.randint(2,10))
Keep running it continuously. At one point, randint will print 10, but randrange will never.
Example 5 : Print only even random number in a range :
We can pass one more argument to the randrange function seen above.It is known as step i.e. if we pass 2, it will produce only numbers divisible by 2,for 3 it will produce numbers divisible by 3 etc. Let’s take a look :
import random print("Even Random number : ") print(random.randrange(2,10,2)) print("Random number Divisible by 5 : ") print(random.randrange(0,100,5))
The first one will print only even random numbers in between 2 to 9 and the second one will print only random numbers divisible by 5 in between 0 to 99.
Example 6 : Print a random element in a sequence :
The random module contains one method called choice that takes one sequence as argument and returns one random element of that sequence. If the sequence is empty, it will raise one IndexError.Let’s take a look at the below program to understand this :
import random days = ["sun","mon","tue","wed","thu","fri","sat"] print(random.choice(days))
It will print one random word from the provided input sequence.
Example 7 : Shuffle a list :
Module random also has one method called shuffle that shuffles all elements of a list. For example :
import random days = ["sun","mon","tue","wed","thu","fri","sat"] for x in range(5): print("shuffling..") random.shuffle(days) print(days)
It will print output like below :
shuffling.. ['sun', 'wed', 'mon', 'sat', 'tue', 'fri', 'thu'] shuffling.. ['mon', 'thu', 'fri', 'tue', 'sat', 'wed', 'sun'] shuffling.. ['tue', 'fri', 'mon', 'sat', 'sun', 'thu', 'wed'] shuffling.. ['thu', 'tue', 'wed', 'fri', 'mon', 'sat', 'sun'] shuffling.. ['thu', 'wed', 'mon', 'fri', 'sat', 'tue', 'sun']