# random_func.hpp File Reference

Pseudo random number generator. More...

Go to the source code of this file.

## Data Structures
struct | Randomizer
| Structure to encapsulate the pseudo random number generators. More...
struct | SavedRandomSeeds
| Stores the state of all random number generators. More...
## Functions
static void | SaveRandomSeeds (SavedRandomSeeds *storage)
| Saves the current seeds.
static void | RestoreRandomSeeds (const SavedRandomSeeds &storage)
| Restores previously saved seeds.
void | SetRandomSeed (uint32 seed)
static FORCEINLINE uint32 | Random ()
static FORCEINLINE uint32 | RandomRange (uint16 max)
static FORCEINLINE uint32 | InteractiveRandom ()
static FORCEINLINE uint32 | InteractiveRandomRange (uint16 max)
static FORCEINLINE bool | Chance16I (const uint a, const uint b, const uint32 r)
| Checks if a given randomize-number is below a given probability.
static FORCEINLINE bool | Chance16 (const uint a, const uint b)
| Flips a coin with a given probability.
static FORCEINLINE bool | Chance16R (const uint a, const uint b, uint32 &r)
| Flips a coin with a given probability and saves the randomize-number in a variable.
## Variables
Randomizer | _random
| Random used in the game state calculations.
Randomizer | _interactive_random
| Random used every else where is does not (directly) influence the game state.

## Detailed Description

Pseudo random number generator.

Definition in file random_func.hpp.

## Function Documentation

static FORCEINLINE bool Chance16 | ( | const uint | a,
|  | const uint | b |
| ) |  |  | `[static]`

Flips a coin with a given probability.

This macro can be used to get true or false randomized according to a given probability. The parameter a and b create a percent value with (a/b). The macro returns true in (a/b) percent.

See also:
  Chance16I()

Parameters:

  | a | The numerator of the fraction
  | b | The denominator of the fraction

Returns:
  True in (a/b) percent

Definition at line 140 of file random_func.hpp.

References Chance16I().

Referenced by BuildTownHouse(), ChangeIndustryProduction(), Disaster_Helicopter_Init(), DisasterTick_Submarine(), GrowTownInTile(), IndustryDailyLoop(), IsRoadAllowedHere(), TileLoop_Town(), and UpdateTownGrowRate().

static FORCEINLINE bool Chance16I | ( | const uint | a,
|  | const uint | b,
|  | const uint32 | r |
| ) |  |  | `[static]`

Checks if a given randomize-number is below a given probability.

This function is used to check if the given probability by the fraction of (a/b) is greater than low 16 bits of the given randomize-number r.

Do not use this function twice on the same random 16 bits as it will yield the same result. One can use a random number for two calls to Chance16I, where one call sends the low 16 bits and the other the high 16 bits.

Parameters:

  | a | The numerator of the fraction
  | b | The denominator of the fraction, must of course not be null
  | r | The given randomize-number

Returns:
  True if the probability given by r is less or equal to (a/b)

Definition at line 122 of file random_func.hpp.

Referenced by Chance16(), Chance16R(), and ChangeIndustryProduction().

static FORCEINLINE bool Chance16R | ( | const uint | a,
|  | const uint | b,
|  | uint32 & | r |
| ) |  |  | `[static]`

Flips a coin with a given probability and saves the randomize-number in a variable.

This function uses the same parameters as Chance16. The third parameter must be a variable the randomize-number from Random() is saved in.

The low 16 bits of r will already be used and can therefor not be passed to Chance16I. One can only send the high 16 bits to Chance16I.

See also:
  Chance16I()

Parameters:

  | a | The numerator of the fraction
  | b | The denominator of the fraction
  | r | The variable to save the randomize-number from Random()

Returns:
  True in (a/b) percent

Definition at line 160 of file random_func.hpp.

References Chance16I().

static void RestoreRandomSeeds | ( | const SavedRandomSeeds & | storage | ) | `[inline, static]`

Restores previously saved seeds.

Parameters:

  | storage | Storage where SaveRandomSeeds() stored th seeds

Definition at line 73 of file random_func.hpp.

References SavedRandomSeeds::interactive_random, and SavedRandomSeeds::random.

Referenced by CmdAutoreplaceVehicle().

static void SaveRandomSeeds | ( | SavedRandomSeeds * | storage | ) | `[inline, static]`

Saves the current seeds.

Parameters:

  | storage | Storage for saving

Definition at line 64 of file random_func.hpp.

References SavedRandomSeeds::interactive_random, and SavedRandomSeeds::random.

Referenced by CmdAutoreplaceVehicle().