As much of a tool as an “if statement” or a “for loop” can be for you, generating random numbers can be equally as useful and equally as versatile.

You can use random numbers to create percentage-based occurrences such as loot drop ratios, enemy spawn rates, crit-chances, AI decision making, Procedural Generation, etc.

Here’s an example of how to use random number generation, and track it based on a percentile ratio.

I set range increments of 25 but you can add any amount of different ranges that you feel meet your needs.

## You can use this with “if statements” or with “switch statements”.

This code sequence could be tied to a games looting system. You could give each different item a number range that is associated with its drop chance and for loop the sequence a number of times equal to the number of items you’d like to spawn.

Or you could use it to determine various states of behavior for an AI. Say you wanted your enemies to change the way that they move based on a random chance. This can create an elaborate and more immersive feeling AI, and can very easily be expanded upon.

## [SIDE NOTE]

*Don’t forget that when declaring a range of numbers, that the maximum number that you will get is one less than the maximum number value that you typed since the system begins counting from 0. This is why a percentile range would look like (0, 100) and not (1, 100) which would only result in a 1 - 99 number range. This works similarly for Arrays as well.*