| by Arround The Web | No comments

Python Random Module

This short article is about Python’s random module, which generates synthetic random numbers for many probability distributions. There are several modules in Python, one of which is called the random module. A Python module can be defined as a file consisting of Python definitions and commands. The module defines classes, variables, and functions in Python as executable code. Let’s start with the random module explanation with several examples without further ado.

Random Module

The random module is used to perform actions randomly, such as choosing a random element from a list. It can also be used to perform random actions of generating numbers or printing a random value. A built-in module allows us to generate random numbers for different distributions, such as integers and real numbers. The random module is a library in Python that extends the basic functions of Python.

In the following section, we will discuss some examples of random modules for your understanding.

Example 1

Let us take an example of a list, and then we will print a random value from the list of the following numbers. In the code, you can see that we have created a list, namely “new_list”, after importing the random module as rdm. The list contains eight values. These are 5, 6, 7, 8, 9, 10, 11, and 12. Finally, the rdm.choice method prints the random value from the created list.

import random as rdm
new_list = [5, 6, 7, 8, 9, 10, 11, 12]
print (rdm.choice(my_list))

As previously mentioned, the random module artificially generates a random number. By using the code rdm.choice(my_list), we gave the command to print a random number from “my_list”, and the output we get after running the code is:

Example 2

In this example, we will talk about random floats. We have used the “random.random()” method to generate random floats. When executing the given code, we get an output between the numbers 0 and 1. This code is the basic function of the random module, and almost all functions of this module depend on the random() function.

import random
print(random.random())

After running the code, we get the following result which is a random number generated between 0 and 1:

Let’s talk about random module methods. The random module has a set of methods and functions, several are given below:

Example 3

This example is about the randint method. The randint function gives us a number that is between a defined range. For example, specify a range between 20 and 30. The syntax for this function is random.randint(a, b) where a represents the lower limit and b represents the upper limit; in this case, a = 20 and b = 30.

import random
print(random.randint(20, 30))

As we can see from the previous output, by choosing a range for the random number generator, we get the number 26.

Example 4

Let’s discuss the randrange method. The syntax of this function is random.randrange(start, stop[, step]).

This function does not consider the stop number while generating a random number. For example, if the range is (3, 30, 3), the random number will not consider the stop number 30 and select it as the output.

import random
print(random.randrange(3, 30, 3))

Here is the output:

Example 5

This example will elaborate on the random choice method. This function selects a random item from a sequence provided in a list or string. Its syntax is: random.choice().

In the following screenshot, you can see that we have provided a list of numbers to the random.choice method:

import random
print(random.choice([5, 10, 15, 20, 45, 50]))

We gave the function a choice of several options, and the output we got from it was 45, which is one of the numbers from the string.

Example 6

Let’s discuss the random shuffle method. A generic sequence can be defined as a collection of items in the form of a list. This function introduces a state of randomness to the sequences and shuffles the list in place. The syntax for this function is random.shuffle(x), where x is a sequence or list.

In this example, we will see a random sequence before and after mixing. Using the randint function, we selected a range of numbers and printed the list before shuffling it, and the output we got from it is as follows:

As you can see below, you have the complete code. Here, a range of numbers is created using the randint() function and printed the list before shuffling. After that, we executed the shuffle() function on the created list and printed the result afterward.

import random as rdm
my_list = [rdm.randint(0, i) for i in range(10)]
print('Before shuffling', my_list)
rdm.shuffle(my_list)
print('After shuffling', my_list)

Here, you can see the output before and after shuffling.

Example 7

Let’s discuss another method in this example: a random sample method. This function returns a random sample from the given data. This is the form of either a list or a string. Here, we have created a sample with the name “abc” and printed it as it is. After that, we used the random with all the ranges of values.sample() method and displayed the random values generated.

import random
abc = ['five', 'nine', 'twelve', 'twenty', 'one', 'fifteen']
print(abc)
for i in range (5):
    sample_val = random.sample(abc, 5)
print('random sample:',sample_val)

Running this code will give us five random samples from “abc” as we have defined it in the code sample_val=random.sample(abc,5).

Example 8

This example is about the seed() method. This is a method that calculates a random number generator. The seed value is the number that the number generator needs to generate.

import random
random.seed(49)
print(random.random())

The output we get is displayed below:

Example 9

Lastly, we will explain the uniform method. It is a method that is specified in the Python random library. Nowadays, there is a need to generate random numbers in a range to perform operations. In Python, a built-in method known as uniform() performs the task of quickly generating random numbers in a range.

The syntax of this function is uniform(a, b), where “a” defines the lower limit of the random number and “b” defines the upper limit of the random number that must be generated.

In the given code, two variables are created, having values of 5 and 10. Then, the uniform() method is executed on these numbers.

import random as rdm
one_num = 5
two_num = 10
print("Generated random number between 5 and 10 : ", end ="")
print(rdm.uniform(one_num, two_num))

Attached is the following output:

Conclusion

In this post, we learned about many methods in the Python random module. These methods allow us to work with integers, numbers, strings, and other sequences like lists. Here, we also learned how seed affects sequences of random numbers. This is an easy guide to Python random numbers; to understand them better, run the given examples on your system and try playing with the numbers to see how the functions work.

Share Button

Source: linuxhint.com

Leave a Reply