Setting Boolean values randomly
Perhaps the simplest of all data types is the humble bool. With only two states, true and false, it shouldn't be too hard to set randomly! When represented as integers, the two states have the following properties:
- False = 0 or lower
- True = 1 or higher
Given this, to randomly assign a bool we simply need to generate either the number 0 or 1.
Generating a number between 0 and 1
In Chapter 1, An Introduction to Procedural Generation, we covered the generation of random numbers within a specific range. Well we're now going to put that to use. Using the std::rand()
function we will generate a number between 0 and 1:
std::rand() % 2;
Tip
Remember, std::rand()
generates a number between 0
and RAND_MAX
function. We then calculate ...
Get Procedural Content Generation for C++ Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.