FREE 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 3 / 3
Random numbers
Home >>> Free Online Lessons, Python 3

The seed Function

We mentioned from the beginning that the numbers are pseudo-random, meaning they are generated by complex mathematical calculations.

The function:

seed(optional_integer)

is useful when we want to start the generator from the same value – suitable for testing our programs. If we use the same argument multiple times, we will get an identical series of random numbers each time. Interesting, right?!

EXAMPLE

We use seed() to print five random numbers, and as a parameter, we will specify, let's say, 17:
Editor - lesson_13.py
       
Console/Output done
DETAILS

By specifying seed(), we actually set the seed of randomness, a starting point for the random numbers generated thereafter. If we do not use the seed() function, or call it without the optional parameter, the default argument value is the current system time, so we will have different and random numbers each time.

WHY IS IT USEFUL?

Let's say we are creating a game and using random numbers. To evaluate the program's functionality, the induced randomness, and the analysis of the obtained values, we need to restart the generator each time in test mode so that the numbers are the same when the program is executed. Of course, in the end, when everything works correctly, we remove the seed() function call...

Let's move on to creating some simple, practical applications...
The lesson is now over.
 home   list  CONTENTS   perm_identity   arrow_upward