How to Generate Random Numbers in Python?

In Python, there is a need for random numbers when we are dealing with any data science model or behavior of the program. There is a “random” module that provides different functions to create random numbers in the Python program. This article will provide an overview of creating a random number in Python. The detailed content of this post is listed below:

So, let’s get started!

How to Generate Random Integers in Python?

The “randint()” function of the random module generates a random number within a certain range. Let’s understand it via the following code given below:

Code

#using randint() function
import random
print(random.randint(0,20))
How to install Kali Linux in Window...

In the above code:

  • The “random” module is imported at the beginning of the program.
  • The “randint()” method generates a random number within the given range.
  • The “randint()” method generates a new random number whenever it is accessed.

Output

It can be seen from the output that “13” is the number generated from a range (0, 20).

Using random() Method

The “random()” method of the random module generates any random number without specifying any range. Let’s understand it via the following code:

Note: The default value of the range is (0,1), so the “random()” function generates a random number between 0 and 1.

Code

#using random() Method
import random
val = random.random()
print(val)

In the above code:

  • The “random” module is imported at the beginning of the program.
  • The “random()” function generates any random number without getting any range.

Output

It can be seen that the number “0.369961…” number is generated by the “random()” function.

Using for loop

The “randint()” method can also be used with the “for loop” and “append()” functions to generate the random number value. The “append()” function adds the random number to an empty list. Let’s understand this method via the example of code given below:

Code

#using for loop
import random
random_list = []
for i in range(0,5):
    x=random.randint(1,20)
    random_list.append(x)
print(random_list)

In the above code:

  • The module named “random” is imported into the program.
  • An empty list named “random_list” is initialized.
  • The “for loop” iteration is performed on the range element (0-5). This means that the “5” element position is created in the list for random numbers.
  • The “randint()” method creates a “5” random number from the range (1-20).
  • The “append()” method adds the random number value into the empty list named “random_list”.

Output

The output shows that five (5) random numbers are generated that lie between “1” and “20”.

Using random.sample()

The “sample()” method also generates any random number within a specific range. The “sample()” method generates “multiple” random numbers within a specific range that is not possible in our previous method named the “randint()” method.  Let’s understand this method via the following code:

Code

#using random.sample()
import random

random_list = random.sample(range(0, 50), 6)
print(random_list)

In the above code:

  • The “random” module is imported in the program.
  • The “random.sample()” function takes two parameter values in its parenthesis. The first parameter value takes the range value (0,50), and the second one takes the value of the random number “6” generated in the list.

Output

The above output shows the random number created from the range (0-50).

That’s all from this Python guide!

Conclusion

In Python, the random module function “randint() function”, “random() method”, “random.sample() method” and “for loop with randint()” function are used to generate random Numbers. The “randint()” and all other functions generate random numbers within the specific range. The “random()” method generates any random number without specifying a range. This guide has provided a detailed demonstration of Python’s Random Number Generation.