Random numbers are often needed in games. They are created by pseudo-random generators that create a sequence of apparently non-related numbers. In fact the next number depends on the last and one use a seed value to set the start number. The sequence will always be the same with the same seed. One common way to initialize the seed value is to use the current system time.
Number in a range
Its common for random functions (ex rand) to return a value between 0 and a very high positive number. To get a value inside a certain range use modulo operator and the initial value of the range.
rand() % 100 + 1; // To get a value between 1 to 100.
- Counting bits the normal distribution - 2017
- Generating random numbers from a specific distribution by inverting the cdf - 2017
- Generating random numbers from a-specific distribution with rejection sampling - 2017
- The surprisingly many subtleties of designing crits - 2014
- How chance in games is rarely just a roll of the dice - 2014
- rand() Considered Harmful - 2013
- Probability and Games: Damage Rolls - 2012
- rand() Considered Harmful - 2019