Python Random Number Generation

Python offers a random number generation module. The name of the module is “random”. In the “random” module, we have a set of various functions that are used to create random numbers. Sometimes we need to generate random numbers while performing simulated experiments, in games, and many other applications. This article will explain the random number generation in Python using the various function of the “random” module.

So, let’s get started.

Random number generation functions

The “random” module of Python provides a variety of functions to manipulate and create a random number.

Following are the various common functions that are used for random number generation and manipulation:

Function Description
randint(a,b) The randint() function generates and returns a random number within a given range. The a,b is the range .for example if we write randint(1,7), then this function will return the random number value between 1 and 17.
choice() The choice() function selects and returns the element from the given numbers. The numbers could be in a container i.e. list.
random() The random function produces a random float number and returns the random float number between 0 to 1.
randrange() The randrange() function produces a random number within a sequence. It takes the start value, end value, and a number that you want to exclude from your choice.
shuffle() The shuffle() function takes the container or list as an argument and changes the sequence of the elements.
uniform() The uniform() function returns the float random number in a given range.

These are the most common function to generate random numbers in Python. Let’s look at the case now.

The randint(a,b) function
Following is the example of randint(a,b) function. In the given example, we specify the range between 1,20 and generate a random number. I will run the program multiple times so that we can obtain different random numbers in the given range.

# importing the random module
import random
# printing the random number between 1 to 20
print("The random number is: ",random.randint(1,20))

Output

If we change the range of the randint() function between 1,10 then you will see that the random number will be generated between 1 to 10. I will run the program multiple times so that we can obtain different random numbers in the given range.

# importing the random module
import random
# printing the random number between 1 to 10
print("The random number is: ",random.randint(1,10))

Output
In the output, you can see that the random numbers are generated between 1 and 10.

The choice() function
As discussed earlier that the choice() function selects and returns the element from the given numbers. The numbers could be in a container i.e. list. Let’s see the example of the list.

# importing the random module
import random
# defining the list of numbers
list=[1,2,3,4,44,5,65,99,10,100]

# printing the random choice
print(random.choice(list))

Output
The program is executed multiple times so that we can obtain different random choices from the given list.

Similarly, we can define a list of strings and make the random choice by using the choice() function.

# importing the random module
import random
# defining the list of words
list=["Hello","Welcome","to","the","linuxhint"]

# printing the random choice
print(random.choice(list))

Output
The program is executed multiple times so that we can obtain different random choices from the given list.

The random() function
The random function produces a random number of floating-point from 0 to 1.

# import module
import random
# printing the random floating-point number
print(random.random())

Output
The program is executed multiple times so that we can obtain different random choices from the given list.

If we want to take the sum of one random number between 1 to 10, and one floating-point number, then we can do it in this way.

# importing the random module
import random
#declaring num1 variable and storing random number between 1 to 10
num1= random.randint(1,10)
num2= random.random()
# printing the sum of num1 and num 2
print("The sum is: ",num1+num2)

Output
Most probably the new random numbers are generated every time, therefore the program is executed multiple times to obtain different sum values.

The randrange() function
As discussed earlier, the randrange() function produces and returns a random number between a given sequence. It takes the start value, end value, and a number that you want to exclude from your choice. randrange() function.

# importing the random module
import random
#printing the random number between  1 to 10 and excluding number 2
print(random.randrange(1,10,2))

Output
The program is executed multiple times.

The shuffle() function
The shuffle() function takes the argument of list or container and changes the sequence of the elements.

# importing the random module
import random
# defining the list of numbers
list=[1,2,3,4,44,5,65,99,10,100]
#printing the origional list
print("The origional list is \n", list)
#shuffling the list by calling the shuffle() function
random.shuffle(list)
print("The shuffled list is \n",list)

Output

Similarly, we can shuffle the list of words as well by using the shuffle() function.

# importing the random module
import random
# defining the list of words
list=["Hello","Welcome","to","the","linuxhint"]
#printing the original list
print("The original list is \n", list)
#shuffling the list by calling the shuffle() function
random.shuffle(list)
# printing the shuffled list
print("The shuffled list is \n",list)

Output

The uniform() function
The uniform() function returns the float random number in a given range.

# importing the random module
import random
#printing the random floating-point number between 1 to 10
print(random.uniform(1,10))

Output
The program is executed multiple times to generate multiple floating-point numbers between 1 to 10.

Conclusion

This article explains the random number generation in Python with simple examples. There are various functions in Python for random number generation i.e randint(), random(),etc. This article will help the beginners to understand the random number generation in Python.



from Linux Hint https://ift.tt/3m22BHY

Post a Comment

0 Comments