Random Number Generation
Chapter 5
The header file cstdlib
contains a the rand()
function which returns a pseudorandom number (i.e., a seemingly random number).
rand()
returns anint
value between0
andRAND_MAX
(often32767
).- Examples:cpp
rand() % 100 // a "random" integer >= 0 and < 100` rand() % 100 + 900 // a "random" between 900 and 999 inclusive`
srand()
seeds the random number generator. It accepts 1 parameter, anunsigned int
, which determines the set of random numbers to be returned byrand()
.- Commonly the time is used to seed the random number generator. That way you have a different seed every time the program executes.cpp
const unsigned int SEED = time(nullptr); srand(SEED);
- Generally, you should only call
srand()
once in your code so the numbers appear to be more random.
- Commonly the time is used to seed the random number generator. That way you have a different seed every time the program executes.
This is a fun example that I think will help solidify some concepts for you. Also, it shows how you can use pseudo-random numbers to create a game.
“The lot is cast into the lap, but its every decision is from the LORD.” - Proverbs 16:33 (ESV)
- Random just means WE cannot predict the outcome.