INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 13
PAGE 1 / 3
Random numbers
Home >>> Online Lessons, Python 3

Randomness

Many of you may have played Lotto 6/49, where predicting those six valuable numbers is impossible... even with all the statistics in the world, only a stroke of luck! can lead us to guess them, considering there are 13,983,816 possible combinations:


Above are 6 random numbers between 1 and 69, displayed each time you refresh the page! Good luck!

Similarly, computer games contain random numbers, so each time it seems everything is different, a new scenario, a new set of colors, balls, or enemies appearing from different positions:



Bot Kyle from CS:GO ... often snipes you with the AWP from classic positions on the map, doesn't he? Of course, he has certain locations he estimates based on many factors, there are advanced A.I. algorithms, but... he's often in different spots, right?

A random number is essentially one that cannot be predicted by the user. In fact, behind it, there is a generator (a function) that gives us such a number each time it is called. They are pseudo-random because they are created through complex mathematical calculations.

Very cool! The website [random.org] uses atmospheric noise and complex calculations to generate true random numbers, and the site is used in many fields of activity.

How do we generate them in Python 3?

The set of functions that the Python language contains by default is not very large, but it is extended through modules as you have seen in the case of [turtle].

One of these is random, created specifically to generate random numbers. To use its functions, at the beginning of the program we will write "import random", which loads the module and gives us access to it:
Editor - lesson_13.py
       
Console/Output done
IMPORTANT NOTES

We imported the random module, then printed sets of five random numbers using the for statement for iterations.

To call a function from a module, we write:

module_name.desired_function

First, we used the random() function, which generates a real number of type float, in the range [0.0, 1.0) – the function does not accept parameters.

Of course, these numbers are not very useful to us... How can we display random integers between 0 and 100? Or between 0 and 10000?

Integers

If 0.483421 was generated... Multiplying it by 100, we get 48.3421, which then rounded to an integer becomes 48.

Multiplying it by 10000 and rounding it, we get 4834...

But from 1 to 100? Simply, we add 1 to the value generated up to 99, as shown below:

round(random.random() * 99 + 1)

Integers from an Interval

How can we generate integers between 20 and 70, for example?

We can print, as before, numbers between 0 and 50. Just add 20 to each number:

round(20 + random.random() * 50)

Let's examine other functions of the random module in the following sections.
Proceed to the next page.
 home   list  CONTENTS   perm_identity   arrow_upward