The Random Library in Python

Search for a command to run...

No comments yet. Be the first to comment.
If you have worked with the Solana Wallet Adapter before, you will know that it is very easy to set up a Connect Wallet button with a decent modal. However, customization is pretty limited. We can only add some custom CSS hence changing the styles bu...

And CandyPay QR Codes which people can scan to claim the NFT in under 15 seconds

As developers, most of us use the terminal to interact with our computers for many tasks as we find it more productive. We are familiar with commands like ls, cd, cat, grep, and find. These are primarily pre-installed on our computers and mostly get ...

Most of us use version control systems (mostly git) for our projects but the git CLI is unproductive. We often need to run multiple commands and got to type more characters. Well, what if I told you there are tools that can improve this significantl...

Authentication in Web3 is extremely easy but supporting all the wallets and making a nice UI can be painful and time-consuming. Thankfully, there are many libraries which makes this extremely easy as well. Today we are going to be looking at adding R...

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 pip
We need to import the library though so let us do that real quick
import random
random.choice()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 random.choice() first
This will pick a random item from any iterable object (list, string etc.)
Example:
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
So in the above image we can see that each and every time the function is run, it will print out a different value as it is picking out randomly
random.randrange()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
Example:
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?
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.
random.random()Now this one is a pretty simple one. It returns a float value between 0 and 1
Example:
import random
print(random.random())

random.seed()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.
Example:
import random
random.seed(10)
print("A mapped random number with seed 10 is: " + random.random())

random.shuffle()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.
Example:
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)

random.uniform()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
Arguments:
Example:
import random
print(random.uniform(10, 15))

random.randint()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.
Example:
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!!!
Link to random package docs if you want to explore more - https://docs.python.org/3/library/random.html