What's in Random Module in Python

random - the random module

In this post I want to describe the use of the random module in Python. random translates as chance. The random module provides access to functions that support many operations. Perhaps the most important thing is that you can generate random numbers.

When can you use it?

In case we want the computer to pick a random number in a certain range

How random is random? This is a strange question, but one that is paramount in cases where information security is concerned. When generating random data, strings, or numbers in Python, it is wise to have at least a rough idea of ​​how that data was generated.

In this section, you'll cover a few options for generating random data in Python, and then build a comparison of each in terms of security level, versatility, purpose, and speed.

First of all, a clear disclaimer is required. Most random data generated with Python is not entirely random in the scientific sense of the word. Rather, it is a pseudo-random: it is generated using a pseudo-random number generator (PRNG), which is essentially any algorithm for generating seemingly random, yet reproducible data.

"Real" random numbers can be generated by a real random number generator (TRNG). An example is to repeatedly take a cube off the ground, toss it in the air, and let it land as possible.

Assuming your roll is unbiased, you really have no idea what number the die will land on. Rolling the dice is a simple form of using hardware to generate a number that is not deterministic at all. (Or you can have the Würfel-O-Matic do it for you.) TRNGs are not included in this article, but are still worth a mention for comparison.

PRNGs, which are usually run in software rather than hardware, work a little differently. Here is a brief description:

You start with a random number known as a seed and then use an algorithm to generate a pseudorandom sequence of bits from it.

The random module is imported:

import random

Let's look at some of the basic features of random. You generated a random float above. You can get a random integer between two endpoints in Python with the function random.randint () to generate. This spans the entire [x, y] interval and can include both endpoints:

>>> import random
>>> random.randint (0.10)
>>> random.randint (1.99)

With random.randrange () you can exclude the right side of the interval, ie the generated number is always within [x, y) and is always smaller than the right end point:

>>> random.randrange (1, 10)

If you need to generate random movements that are in a certain [x, y] interval, you can use random.uniform (), which results from the continuous uniform distribution:

>>> random.uniform (200,300)

To select a random item from a non-empty sequence (such as a list or a tuple) you can random.choice () use. There is also random.choices () to select multiple elements from a sequence with replacement (duplicates are possible):

>>> entries = ["one", "two", "three", "four"]
>>> random.choices (entries)
>>> random.choices (entries, k = 2)
['two one']

To simulate sampling without replacement, use random.sample ():

>>> random.sample (entries, 4)
['three', 'four', 'two', 'one']

You can randomize a sequence directly with random.shuffle (). This changes the sequence object and randomizes the order of the elements:

>>> random.shuffle (entries)
>>> entries
['three', 'one', 'four', 'two']

An example for a simple application of a random: Here a number between 1-10 is set and after entering the number is compared.

# / usr / bin / python3
import random
print ("Please guess the number and enter it")
input = int (input ())
number = random.randint (0.10)
if input> number:
print ("Too small")
if input print ("Too big")
if input == number:
print ("Yes, Right")