Pseudo-random numbers:

advertisement
Pseudo-random numbers:
We've seen rand() returns an integer in the range 0 to some very big number,
which depends on the implementation. But this number has a name, RAND_MAX.
If you want a random number in the range 0 .. 19, then you can use the expression
rand() % 20.
The mod operator (%) returns the remainder after integer division.
For example, 10 % 3 returns 1, because 1 is the reminder after 10 is divided by 3.
And 10 % 5 returns 0. Notice that M % N will always return a number in the range
0 .. N-1.
If you want a number in the range 1 .. 6, use 1 + rand() % 6.
In this way you can get a random integer in any range M .. N:
The total number of possible answers is N-M+1.
So M + rand() % (N-M+1) will give you an integer in the range M .. N. Note that
rand() % (N-M+1) has a minimum value of 0 and a maximum value of N-M.
Therefore the minimum value of M + rand() % (N-M+1) is M+0 = M,
and the maximum value is M + (N-M) = N.
The point is not to memorize that formula, but see how we got it, so that you can
come up with formulas of your own.
We now know how to generate a random integer in any range we like.
Sometimes we need a random floating-point number. Examples: random velocity (in
some range), random heading, random temperature, ...
First consider a simpler problem: How do we get a random double between [0, 1]
(this means all numbers between 0 and 1 inclusive).
Answer: divide rand() by RAND_MAX.
A casting changes the type of an expression, for example,
(double) rand()
changes the type of rand() from int to double. This is an explicit and readable way
to change the type of something.
The function srand(x) reseeds the random number generator to x. srand(1) reseeds
it to its default position.
To reseed it to an unpredictable location in the series, we need to call srand() with
an unpredictable number. One common choice is time(NULL), which returns the
number of milliseconds since some date long ago.
#include <ctime>
srand(time(NULL)); // randomize random number generator
Conditional Statements:
A conditional statement selects one from several possible paths of execution based
on some condition. The most important C++ conditional statement is the if.
Two-branch if-statement:
if ( condition ) { // true branch
S1;
S2;
....
Sn;
} else { // false branch
F1;
...
Fm;
}
One-branch if-statement:
if ( condition ) {
S1
...
Sn
}
N-branch (or chained) if-statement:
if ( cond-1 ) {
statements
} else if ( cond-2 ) {
statements
} else if ( cond-3 ) {
statements
} else {
statements
}
As a general rule, if-statements can be nested in any way you like, provided you
surround the statements by { }.
Conditions:
The sorts of things you test to make a decision in if-statement (but also a for-loop
and a while-loop and other places).
Example: N > 0, X < 3.14, Z >= 0.0, A <= 0.0,
B != 1 (not-equals), C == 1 (equals).
You do these kinds of comparisons on ints, doubles, chars, and many other kinds of
things.
N.B. (Note bene!): = (assignment operator) is not the same as == (the equality
operator).
N = 1; // takes the number 1 and puts it in variable N
N == 1 /* takes the value of variable N , compares to 1, and returns true or false.
Doesn't change N */
In general, the compiler will not catch this error, so it's a real trap. (See example
program T4a.cpp on schedule page.)
Download