You might need your program to choose a random lucky winner from a list of people participating in a lottery or maybe you want to chooses a random fruit from a list of fruits the doctor made you. There are many use cases for this library and let us see how we can use this library.
The library is included with the normal python installation so you don't need to install using
We need to import the library though so let us do that real quick
Now that we have imported the library, let us quickly go through some of the common and important functions the library comes with
Let us see
This will pick a random item from any iterable object (list, string etc.)
import random list = ["banana", "apple", "guava", "cherry", "strawberry", "mango"] print(random.choice(list)) # Will pick any random fruit from the list and print that str = "Coronavirus cases have spiked again!!!" print(random.choice(str)) # Will pick any random character from the string and print it out
Now this is an interesting function and probably the one most are looking for. It basically picks out a random number from a range of numbers
import random print(random.randrange(1, 20, 2)) # Now it will print out any random number amongst 1, 3, 5, 7, 9, 11, 13, 15, 17 and 19
There are 3 arguments taken in of which the first two are compulsory and third one is optional and be default
1. But what are the arguments?
- The first argument is the number from which the range starts
- The second argument is the number where the range ends
- The third argument is the step which is the number of numbers to skip while making the list of the numbers
Now does that sound similar?
Yes, this are the exact same arguments take in by the
range function which is in-built in Python. The
random.randrange() function basically creates a list from the range with the arguments given and then picks out a random.
Now this one is a pretty simple one. It returns a float value between 0 and 1
import random print(random.random())
Now this one is a bit tough to understand. So the seed function is basically used to save the state of a random function so that it can generate some random numbers on multiple executions of the code on the same machine or on a different machine for a specific seed value. The seed value is the previous random value generated by the generator or if run for the first time, it is set to the current system time.
import random random.seed(10) print("A mapped random number with seed 10 is: " + random.random())
As the name of the function suggests, we can use it to shuffle a list i.e. change the position of the items in the list.
import random cities = ["London", "Bangalore", "Delhi", "Mumbai", "Tokyo", "New York City"] print("List before shuffling: ") print(cities) print("List after shuffling: ") random.shuffle(cities) print(cities)
This one is pretty similar to
random.randrange() but then returns a
float value instead of an
integer value and does not take any step in input
- Lower Limit - included in generation
- Upper Limit - Excluded in generation
import random print(random.uniform(10, 15))
Takes 2 arguments as the lower limit and the upper limit. The returned number is between these 2 values and can be anyone of them as well.
import random random.randint(50, 100)
So that was some of the most important and commonly used functions of the
random package and that is it for this blog!!!