revised: 10/17/2015, 06/15/2017
These puzzles involve random numbers.
Random numbers are used in
games, in simulation, in testing, and in many other situations.
The random number generator
rand() is part
of the standard library.
Include the header file
stdlib.h in your
source file when you use it.
The prototype for it is:
Each call to
an integer randomly selected from the range
0..RAND_MAX. On many
RAND_MAX is 32767, but on some systems it can be much higher.
rand() is like throwing a 32768-sided die.
The return value is in the range 0 to 32767, but the particular value
is completely unpredictable from throw to throw.
In the GNU library,
RAND_MAX is 2147483647,
which is the largest signed integer representable in 32 bits.
In Microsoft's VCC,
RAND_MAX is 32767.
an algorithm and so the integers it returns are completly predictable.
If you know the algorithm and the previous value returned, the next value can easily be calculated.
Because they are predictable, the numbers are called pseudo-random.
But for many purposes the numbers are
scrambled up enough that they can be used as random numbers.
int srand( unsigned int seed )
seed starts the random number generator at an integer of your choice.
From then on, the sequence is completely determined by the algorithm.
Everytime you start the random number generator with the same seed, you get the same sequence.
To get a different sequence of pseudo-random numbers each time you run a program,
start each run
with a different seed.
rand() produces the same stream of pseudo-random numbers
every time it is used.
This can be desirable since sometimes you wish to test different algorithms
using the same sequence of (pseudo-)random events.
For example, to compare two sorting algorithms you would want to use the same
sequence of unsorted numbers for each.