I’m guessing that’s a mistake.
Using integer arithmetic 7 / 8
is 0
, and 0
is a value you shouldn’t pass to srand
because it upsets a lot of PRNGs.
(There’s a lot of things I don’t like about the tutorials.)
Other people have explained the basic idea, but I want to give a deeper explanation that will hopefully make things clearer. (You’ll probably already know some of this is you started reading about them,
but it might be useful to others as well.)
The kinds of ‘random number generators’ used in programming usually aren’t actually random,
they’re actually pseudo-random, which means they’re generated with an algorithm.
They tend to start with a ‘seed’ value and apply a particular operation to that seed to get their first value,
and from then on they just keep reapplying the same operation.
For example, let’s say we had the worst PRNG in the world and the operation was state + 1
.
If you passed in 8
as the seed, the first value you’d get back is 9
, and then 10
and then 11
et cetera,
because each time the PRNG is just applying the same operation to the internal state and giving you that value.
The state + 1
thing is a bit of a simplification.
A more realistic example would be (((state * multiplier) + increment) % modulus)
,
(where multiplier,
increment
and modulus
are constant values),
which is what a linear congruential generator does.
LCGs are actually notoriously bad random number generators because there’s actually a pattern to the numbers they produce, but it’s hard to spot unless you’re looking, and they’re a good introduction to PRNGs.
(All the good PRNGs are far more complicated.)
Some of the more advanced PRNGs are a lot more complex and have several different parts to their state.
The one used by Arduino is relatively simple and probably not very ‘random’ statistically,
but it’s probably good enough for general use and certainly good enough for games.
Since random
has been mentioned, there are three differences between rand
and random
:
-
rand
returns an int
(16-bits on Arduboy/AVR), random
returns a long
(32-bits on Arduboy/AVR)
-
random
is a non-standard function (and it isn’t available on some Arduino-compatible chips), but rand
is part of the C standard library
- The Arduboy is actually programmed with C++, but most of the Arduino libraries are actually C libraries for some reason
-
rand
doesn’t accept any arguments, it just returns a value, whilst random
has three variants, one that takes no arguments, one that takes an exclusive upper bound and one that takes an exclusive upper bound and an inclusive lower bound