Introduction to Python Random Module
Python random module is an inbuilt module of python that is used to generate random numbers in python. Modules are a collection of codes or classes or a set of rules that are predefined in python. We just need to use import keywords to use all these classes or codes. Once we import a random module, we can access all the functions or classes that are defined into it. Modules are also called libraries.
There are many inbuilt functions available in the random module. We can use those functions according to our requirements and the type of random number we want to generate.
- function_name: function available in the module
- attr* : this parameter that function takes, it might be optional in a few of the random functions.
Examples of Random Module in Python
Lets us discuss Examples:
This is a very basic example of a random module function. We have used random functions, and there is no parameter in the function. Random() generates random floating numbers between 0.0 and 1.0. This function does not take any parameters.
In the above example, we have used randint function. This function takes two parameters, starting value and ending value. Parameters should be of integer type; if we pass floating point number, then this function will return an error. We have 1 as starting number and 5 as the ending number in the above function, so the function will return a random number between 1 and 5. Both parameters can be the same number, but the first number should be smaller than the second number; otherwise, it will generate an error.
In the above program, we have used randrange function. If we break this function rand + range. As we all know range function returns the list of integer value depending upon and starting and ending value and step value. Here 1 is a start, 10 as an end and 2 as a step value. Internally a range function will be executed that will generate the list of numbers like 1,3,5,7,9. Now random will pick any random value from this list only.
In this example, we have specified start as 0, end as 100 and step as 20. So randrange function will pick random from the range of these start, end and step.
In the above program, we have used the choice method of random modules. This function takes a single parameter. This parameter might be String or List or dictionary also, and it will return a random value from that parameter. We have passed a string in the method, and it will random character from the string and return it as an output.
In the above program, we have passed a list inside the choice method, and the output will be one value out of these list values.
list = [1,2,3,4,5] random.shuffle(list)
In the above program, we have used the shuffle method of the random module. This method does the shuffling of the sequence that we passed into it. It takes two parameters, out of which one parameter is optional. The disadvantage of this method is that we lost the original ordering of the sequence or the list.
list = [1,2,3,4,5] num=random sample(list,len(list))
list = [1,2,3,4,5] num = random.sample(list,len(list))
We have used a random sample method in random in the above program. This method takes two parameters; the first parameter is a sequence and the second parameter is a length of sequence that we want to return the output. The second parameter is also mandatory; it can be equal to the length of the list or less than the length of the list but couldn’t be greater than the list. This method keeps the original sequence and creates a new sequence. We can store the random sequence into another variable while keeping the original sequence as it is.
random.choices(population, weights=None, *,cum_weights=None, k=1)
list = [fruit","vegetable","juice","drink] print random.choices(list, weights = None, k = 10))
This method takes 3 parameters; all parameters are mandatory. The first parameter will be our sequence, which might be a range or list of values. The second parameter is the weight of the values that need to be accumulated, and the third parameter number of items to be returned.
print(random.choices="auto">(list, weights =[6,1,1,1], k = 10))
If weight is passed as none items are generated randomly. If we pass the weight, then weight items should match the count of list items.
Python random module is a very useful module; it provides so many inbuilt functions that can be used to generate random lists and mostly used for generating security token randomly and range of list.
This is a guide to Random Module in python. Here we discuss the introduction along with different examples and its code implementation. you may also have a look at the following articles to learn more –