Repeating loops

Another use of generating random numbers is to loop over certain code an undetermined number of times. For example, when we spawn our items we make individual calls to the spawn code. This is fine if we just want to spawn one item every time, but what about when we want to spawn a random number of items.

We need to make a random amount of calls to our code, which we'll later wrap in its own function, and this can be achieved using for loops. In a for loop we specify how many times we want the loop to iterate, so instead of using a fixed value as we normally would, we can generate a random number to use instead. Each time the code is run, a new random number will be generated, and the loop will be a different size each time.

You can ...

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.