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:
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...