Random number in python 3

by Alex
Random number in python 3

Like many other programming languages, Python allows you to work with random value generators. You can use them to quickly create sequences of different numbers or symbols that cannot be predicted. For this purpose, Python uses a built-in library with many methods for controlled generation.

What are random numbers?

Random numbers are random data that have been generated automatically by a computer program. Such information is used in many types of software where it is necessary to deal with unpredictable quantities. A striking example of this are slot machines or casinos, in which a new winning combination of numbers is generated each time. This approach is also used for cryptographic purposes to create strong passwords. It is worth noting that the standard Python tools are not capable of providing truly random values in a program. They provide a pseudo-random sequence. It is initialized from some random number. That is, if we initialize the sequence with the same number, it will give the same data every time. To prevent this, the value of system clock is taken for initialization.

But there are mechanisms that allow you to get a completely new and independent sequence of numbers. They have a significant disadvantage. It significantly reduces the performance. This is due to the fact that the time to get each number increases by several times.

So usually uses the method of generating pseudorandom values with initiation from the system clock.

Random number implementations in Python

The Python programming language contains several different modules used to generate pseudorandom numbers. All of them usually use the current system time, which is set on the computer, for their purposes. This ensures that a different sequence of values is obtained at each new call to the generator. Among the tools that are designed to work with pseudorandom numbers is the rather extensive random library, as well as the functions numpy.random and os.urandom. Features of their application:

  • The standard random module in Python 3 includes many methods for generating both integers and real numbers, as well as sequences with certain parameters.
  • The function numpy.random is used to fill arrays with random values.
  • The os.urandom function provides a set of random bytes, which is applicable in cryptography.
The random module has a SystemRandom class, inside the implementation of which goes to just os.urandom. This alternative generator implements the same functions, but they can already be used in cryptography.

The random library is the most widely used in Python. That is why we are going to study it in details.

The random module

Below is a table describing the most important methods of the plugin module, which is part of the standard Python libraries. The table lists the names of the functions, as well as the available list of parameters with a small characteristic.

Method Feature
random() returns a number between 0 and 1
seed(a) sets the generator to a new sequence of a
randint(a, b) returns an integer in the range of a and b
randrange(a, b, c) returns an integer in the range a to b in increments of c
uniform(a, b) returns a real number in the range of a and b
shuffle(a) shuffle the values in the list a
choice(a) returns a random element from the list a
sample(a, b) returns a sequence of length b from the set a
getstate() returns the internal state of the generator
setstate(a) restores internal state of the generator a
getrandbits(a) returns a randomly generated a bits
triangular(a, b, c) returns a real number from a to b with a distribution of c

Here we would like to describe the seed function. It is exactly what is used to set the initializing number of a pseudo-random sequence. When seed is called without parameter, the value of the system timer is taken. This function is called in the constructor of the Random class. In the examples, we will see how the basic functions are applied. And at the end, we’ll see how SystemRandom is used.


To take advantage of random capabilities in Python 3 you need to import the random library by placing it at the beginning of your executable with the import keyword.

Real numbers

The module has a random function with the same name. In Python it is used more often than the other functions in this module. The function returns a real number between 0 and 1. The following example demonstrates the creation of three different variables a, b and c.

import random
a = random.random()
b = random.random()


Integer numbers

The randint function is used to generate random integers within a certain range, taking two arguments: a minimum value and a maximum value. The program below shows the generation of three different values between 0 and 9.

import random
a = random.randint(0, 9)
b = random.randint(0, 9)


Ranges of integers

The randrange method allows you to generate integer values by working with three parameters: minimum and maximum value, and step length. By calling the function with one argument, the initial boundary will get the value 0 and the interval will become 1. For two arguments, only the step length is automatically initialized. The following example shows how this method works with three different sets of parameters.

import random
a = random.randrange(10)
b = random.randrange(2, 10)
c = random.randrange(2, 10, 2)


Ranges of real numbers

To generate a real number, we use a method called uniform. It takes only two arguments, a minimum and a maximum value. You can see how it works in the following code sample, where the a, b and c variables are created.

import random
a = random.uniform(0, 10)
b = random.uniform(0, 10)


Using in generators

Pseudorandom number generators can also be used to create sequences. In the following code snippet, a set of numbers is created using a list generator with random filling and length. As you can see, in this example the randint function is called twice: for each item and for the size of the list.

import random
a = [random.randint(0, 9) for i in range(random.randint(0, 9))]

[4, 1, 8, 6, 6, 9, 7]


The shuffle method lets you shuffle the contents of an already created list. Thus, all of its items will be in an absolutely random order. An example, that shows how this function works with a list of 10 values, is

import random
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[3, 9, 0, 7, 6, 2, 5, 1, 8, 4]

Random list item

With the choice function, you can extract a random item from an existing dataset. In the following example, the variable b gets some integer from the list a.

import random
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = random.choice(a)


Multiple list items

You can extract not just one element from a sequence of data, but a whole set of values. The function sample allows you to get a completely new list of numbers from the random components of an existing list. The original sequence should be entered as the first argument, and the desired length of a new array should be specified in place of the second argument.

import random
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = random.sample(a, 5)

[8, 0, 9, 4, 6]

Letter generation

The capabilities of the standard library allow you to generate not only numbers, but letters as well. The following example shows how to initialize three different variables with random Latin characters. To do this you must import the string module and then use the list of letters that includes all the letters of the English alphabet.

import random
import string
a = random.choice(string.letters)
b = random.choice(string.letters)
c = random.choice(string.letters)


As you can see, the letters are displayed in different case. To convert them to a common case, it is recommended to call the standard upper or lower string methods.


As mentioned earlier, SystemRandom is based on os.urandom. It also outputs pseudo-random data, but it depends additionally on the operating system. The results are used in cryptography. The disadvantage is that SystemRandom functions take several times longer to complete. Let’s see an example of using it:

import random
sr = random.SystemRandom()
a = sr.random()
b = sr.randint(0, 9)
c = sr.randrange(2, 10, 2)



Thus, the Python programming language contains a lot of built-in methods for generating and processing random values. You can use them with the help of various libraries included in the standard toolkit of the platform. Thanks to these functions you can set different conditions, as well as restrictions for your generators.

Related Posts