Sometimes it happens that we lack data and need to make up a bunch of new examples rather quickly. Of course, you can spend some time writing those examples yourself, but it's not so efficient, right? It would make more sense to shift the responsibility to your computer, namely, the Python's built-in module random. In this module, a random search is used to generate elements and is performed using an algorithm whose starting point is a seed. The seed allows us to save the current state, making it easy to reproduce the same numbers on several code executions. Therefore, the results given aren't random at all and, technically, this module should have been called pseudo-random. Nevertheless, it may be useful for a large number of applications, such as modeling and simulation.
Random method: first steps
First of all, we need to import the module:
import randomAfter we've managed to do the previous task, it's possible to try the random.random() function that will provide us with a pseudo-random number from 0 to 1:
print(random.random()) # 0.5557276751294531We can also control the pseudo-random behavior by specifying the seed manually, i.e. configure the new sequence of pseudo-random numbers using the random.seed(x) function. You can set your own number or omit the optional argument x and consequently current system time would be used by default.
random.seed()
print(random.random()) # 0.956177930864557Now try to set the x argument. Haven't you noticed the change of the result? If you choose 5, you'll get 0.6229016948897019 as a result, if 20 – 0.9056396761745207, etc. Thus, the seed controls the behavior of pseudo-random in Python and can be used with any other function of the random module.
Random basic functions
Moving forward, other useful functions are:
random.uniform(a, b)– returns a pseudo-random float number in the range between a and b:
print(random.uniform(3, 100)) # 35.94079523197162random.randint(a, b)– returns a pseudo-random integer number in the range between a and b. Note that the last element of the range is included too (in contrast torange())!
print(random.randint(35, 53)) # 52
print(random.randint(7, 9)) # 9random.choice(seq)– returns a pseudo-random element from non-empty sequences:
print(random.choice('Voldemort')) # mrandom.randrange(a, b, c)– returns a pseudo-random number from a range between a (inclusive) and b (exclusive) with a step c. The latter argument allows you to generate random numbers by a given step. In the example below, given the step 5, the function can output any number from 3, 8, ... 98. What is more, just like with therange()function, the start and step arguments may be omitted with the default values 0 and 1 respectively. It means that the function can take one, two, or three parameters:
# Specifying the step argument
print(random.randrange(3, 100, 5)) # 3
print(random.randrange(3, 100, 5)) # 48
print(random.randrange(3, 100, 5)) # 18
# Omitting unnecessary arguments
print(random.randrange(1, 5)) # 2
print(random.randrange(100)) # 44random.shuffle(seq)– shuffles a sequence. Attention: it doesn't work with immutable datatypes! Mind that after applying this function we print the list itself, notrandom.shuffle(tiny_list)since it will result inNone.
tiny_list = ['a', 'apple', 'b', 'banana', 'c', 'cat']
random.shuffle(tiny_list)
print(tiny_list) # ['apple', 'banana', 'a', 'cat', 'b', 'c']
print(random.shuffle(tiny_list)) # Nonerandom.sample(population, k)– returns a pseudo-random k length list from a population sequence. This function is used for random sampling without replacement:
print(random.sample(range(100), 3)) # [24, 33, 91]Furthermore, there are plenty of other functions that are used in common mathematical practice, e.g. random.gammavariate(alpha, beta) that is used for gamma distribution or random.gauss(mu, sigma) that returns the Gaussian distribution. If you need such a narrow-specialized function, you can consult the Python documentation.
The pseudo-random generators of the random module should NOT be used for security purposes. If you are intending to work with passwords, security tokens and other sensitive data, check out the secrets module. It's considered more reliable since it generates secure random numbers.
Summary
To sum up, in this topic, we've looked closely at random module from a Python standard library and its basic functionality. Now you can use it in your projects yourself!