Oct 08, 2022 By Team YoungWonks *
What is Python Random Module?
A random action is one that is carried out without regard for any particular method. Python's random module is built-in. It is used to code by performing actions at random. The probability of selecting anything from the given range remains constant. For example, pseudo-random numbers such as random integers, random decimal values, or selecting a random item from a list can be used.
In this Python tutorial, we'll go over all of the functions available in the Python random module and how to use them in code.
Import the module before using the functions.
Syntax: import random
Python Random Module Functions
1. Random Integers
a. This first random number generator picks integers randomly from a given range.
Syntax: random.randint(start, end)
b. The second method is used to pick a random number from a specified range having the step value as well.
Syntax: random.randrange (start, stop, step)
2. Random decimal numbers(float)
a. This methods picks random floating point number from a given range.
Syntax: random.uniform(start, end)
b. Random decimal numbers from 0 to 1: This method picks random float number between 0 to 1.
3. Random Element Picker
Apart from the above pseudo-random number generators, we can use the random module to pick random element from a list, tuple or string.
The above method picks a random item from the list.
4. Random Element Picker as a list
In order to generate a list of random items we can use the method given below.
Syntax: random.sample(n, list)
Here, n is the required number of items
5. Random Jumble the list
We can randomly jumble the items of a list using the random module.
6. Random Seed
The seed method is used to initialize the random number generator. It also allows us to generate the same random number where the start seed value is always the current system time.
7. Random State
In Python, the Random module includes functions for setting and retrieving the state. It obtains or sets the current thread's state. Threading is the simultaneous execution of multiple smaller operations.
8. Random Bits
We can find the size of the randomly picked integer or element.
9. Random Triangular Values
This method returns a random float number from the specified range using a triangular random function. The third parameter is the mode, which is the midpoint of the numbers specified. It does not provide bias to either extreme value by default. However, if the mode value is included, the generated random number will be biased towards it.
Syntax: random.triangular (start, step, mode)
10. Types of Distribution random values
There are a few other random module functions that are used for statistics. It is used to generate random values based on the type of distribution's. To visualize the distribution, we can use the random float numbers generated to generate a graph with the matplotlib module. This is commonly used in data science algorithms.
Let us now look into these inbuilt methods and their syntax: These methods generate random float numbers between 0 to 1.
a. Beta Distribution: This is a probability distribution of value from a finite range.
Here, we can also observe the beta distribution graphically using matplotlib to understand its applications.
b. Exponential Distribution: This is the probability distribution of the amount of time until specific task is happens.
Mean of the exponential distribution is 1/lambda
c. Gaussian Distribution: This is also known as the normal distribution. This is a bell-shaped curve where equal number of values lie below and above the mean of all the values.
d. Logarithm Distribution: In this method, the natural log of the random values are normally distributed.
e. Gamma Distribution: This method is used to analyze the continuous variables which also has skewed distributions. This is majorly used in data science.
f. Von Mises Distribution: This is also known as the Fisher-von Mises distribution. It is used to specify the maximum entropy distribution of the circular movement.
g. Pareto Distribution: random.paretovariate()
h. Weibull Distribution: In statistics, this probability distribution is used to observe the failure of a phenomenon. It analyses product reliability, life data, and model failure.
Python's random module is one of the most versatile modules in the language. It is used to derive and analyze results in many fields such as data science, statistics, probability, and mathematics. Now that you've learned these methods, you can use them in your code.
*Contributors: Written by Aayushi Jayaswal; Lead image by Shivendra Singh