{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson A13 – Random numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The random module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `random` module in the Python standard library contains the Python random number generator (RNG) and some related functionality. Refer to the official [documentation](https://docs.python.org/3.8/library/random.html) for anything we are not covering in this tutorial. As we will see, the generation of random numbers is not only nice for illustration purposes. Import the module like this:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The module has two fundamental functions: `random.seed()` and `random.random()`. The first sets the seed (the origin) for the RNG. The second draws a sequence of uniformly distributed random numbers in the half-open interval $[0, 1)$. The results differ with each execution of `random.random()` depending on the seed. This becomes a problem if one wants reproducible results, e.g. for debugging. Setting a seed fixes this problem.\n", "\n", "The seed determines the produced sequence of random numbers. So fixing the seed makes the RNG return random numbers that are predictable (which sounds admittedly paradox). This is demonstrated below. We set the seed to zero and print the first five random numbers. Repeating the process three times yields identical sequences, always. This is useful for creating reproducible results that rely on random numbers." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Seed = 0\n", "1 0.8444218515250481\n", "2 0.7579544029403025\n", "3 0.420571580830845\n", "4 0.25891675029296335\n", "5 0.5112747213686085\n", "\n", "Seed = 0\n", "1 0.8444218515250481\n", "2 0.7579544029403025\n", "3 0.420571580830845\n", "4 0.25891675029296335\n", "5 0.5112747213686085\n", "\n", "Seed = 0\n", "1 0.8444218515250481\n", "2 0.7579544029403025\n", "3 0.420571580830845\n", "4 0.25891675029296335\n", "5 0.5112747213686085\n", "\n" ] } ], "source": [ "for _ in range(3):\n", " print(\"Seed = 0\")\n", " random.seed(a=0)\n", " for i in range(5):\n", " print(i + 1, random.random())\n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Further functionality, derived from the basic `random.random()` function, includes:\n", " \n", " - `random.randrange(start, stop, step)`: returns a random element of range(start, stop, step)\n", " \n", " - `random.randint(a, b)`: returns a random integer from the interval [*a*, *b*]\n", " \n", " - `random.choice(seq)`: returns a random element from a sequence\n", "\n", " - `random.choices(seq, k=1)`: returns a list of *k* random elements from a sequence (with replacement)\n", "\n", " - `random.sample(seq, k)`: returns a list of *k* random elements from a sequence (without replacement)\n", " \n", " - `random.shuffle(seq)`: shuffles a sequence in-place (brings the elements in randomly different order)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before shuffling x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "After shuffling x = [9, 0, 3, 5, 1, 8, 2, 7, 4, 6]\n", "Random samples: [0, 1, 3]\n", "Random choices: [5, 7, 4]\n", "Random choice: 8\n" ] } ], "source": [ "x = list(range(10))\n", "print(\"Before shuffling x =\", x)\n", "random.shuffle(x)\n", "print(\"After shuffling x =\", x)\n", "print(\"Random samples: \", random.sample(x, 3))\n", "print(\"Random choices: \", random.choices(x, k=3))\n", "print(\"Random choice: \", random.choice(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "