0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Python is famous for having many<strong>standard libraries</strong>, called modules and packages, with thousands of different functions implemented. It's useful for developers to understand these standard libraries, as this knowledge saves time and effort.</p>
1
<p>Python is famous for having many<strong>standard libraries</strong>, called modules and packages, with thousands of different functions implemented. It's useful for developers to understand these standard libraries, as this knowledge saves time and effort.</p>
2
<p>In this lesson, we'll get acquainted with one of the standard libraries, the random module.</p>
2
<p>In this lesson, we'll get acquainted with one of the standard libraries, the random module.</p>
3
<h2>What is the random module</h2>
3
<h2>What is the random module</h2>
4
<p>When developing programs, you sometimes need to generate random numbers. You can use the<a>random</a>module in Python for this purpose. It provides many functions, but we'll focus on two for now:</p>
4
<p>When developing programs, you sometimes need to generate random numbers. You can use the<a>random</a>module in Python for this purpose. It provides many functions, but we'll focus on two for now:</p>
5
<ul><li>randint generate an integer within a given range</li>
5
<ul><li>randint generate an integer within a given range</li>
6
<li>choice select a random item from the given set</li>
6
<li>choice select a random item from the given set</li>
7
</ul><h2>How to generate random numbers</h2>
7
</ul><h2>How to generate random numbers</h2>
8
<p>To generate a random number, you need to import the randint function from the random module:</p>
8
<p>To generate a random number, you need to import the randint function from the random module:</p>
9
<p>For example, let's try generating a number from 1 to 100:</p>
9
<p>For example, let's try generating a number from 1 to 100:</p>
10
<p>Note that both range boundaries are enabled, so randint can output any value in the range, including 1 and 100.</p>
10
<p>Note that both range boundaries are enabled, so randint can output any value in the range, including 1 and 100.</p>
11
<p>Let's move on to a more complex example:</p>
11
<p>Let's move on to a more complex example:</p>
12
<p>Here, the program selects a random character from the string. That said:</p>
12
<p>Here, the program selects a random character from the string. That said:</p>
13
<ul><li>The string in the string variable has a length of 5</li>
13
<ul><li>The string in the string variable has a length of 5</li>
14
<li>The index of the last element in the line is 4</li>
14
<li>The index of the last element in the line is 4</li>
15
<li>String characters are indexed from zero</li>
15
<li>String characters are indexed from zero</li>
16
</ul><p>If we try to generate a number via randint(0, 5), we will get the value 5. Then the program will generate an IndexError: it won't be able to output the fifth character out of the four.</p>
16
</ul><p>If we try to generate a number via randint(0, 5), we will get the value 5. Then the program will generate an IndexError: it won't be able to output the fifth character out of the four.</p>
17
<p>How do you prevent this? You shouldn't just set the upper limit of the range, but calculate it, i.e., subtract one from the length of the string. It is what we did in the example code above.</p>
17
<p>How do you prevent this? You shouldn't just set the upper limit of the range, but calculate it, i.e., subtract one from the length of the string. It is what we did in the example code above.</p>
18
<h2>How to select random items</h2>
18
<h2>How to select random items</h2>
19
<p>Above, we looked at an example in which we chose a random string character. This task arises quite often, so there's a function called choice in the random module.</p>
19
<p>Above, we looked at an example in which we chose a random string character. This task arises quite often, so there's a function called choice in the random module.</p>
20
<p>If you use this function, selecting a character from the string will look like this:</p>
20
<p>If you use this function, selecting a character from the string will look like this:</p>
21
<p>Using choice, you won't have to think about range boundaries; the function selects items correctly. It is essential to check that the string in question isn't empty. Otherwise, we get the error IndexError: Cannot choose from an empty sequence.</p>
21
<p>Using choice, you won't have to think about range boundaries; the function selects items correctly. It is essential to check that the string in question isn't empty. Otherwise, we get the error IndexError: Cannot choose from an empty sequence.</p>
22
<h2>What is the pseudorandom</h2>
22
<h2>What is the pseudorandom</h2>
23
<p>The random module is based on<strong>a pseudorandom number generator</strong>. The algorithm chooses numbers not randomly but based on complex mathematical calculations. Because of this, we shouldn't use it in situations requiring increased security.</p>
23
<p>The random module is based on<strong>a pseudorandom number generator</strong>. The algorithm chooses numbers not randomly but based on complex mathematical calculations. Because of this, we shouldn't use it in situations requiring increased security.</p>
24
<p>Cryptography, encryption, and other similar applications use the<a>secrets</a>module, based on a less predictable random generation mechanism.</p>
24
<p>Cryptography, encryption, and other similar applications use the<a>secrets</a>module, based on a less predictable random generation mechanism.</p>
25
25