OpenTTD
Data Structures | Functions | Variables
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. More...
 
static void RestoreRandomSeeds (const SavedRandomSeeds &storage)
 Restores previously saved seeds. More...
 
void SetRandomSeed (uint32 seed)
 (Re)set the state of the random number generators. More...
 
static uint32 Random ()
 
static uint32 RandomRange (uint32 limit)
 Pick a random number between 0 and limit - 1, inclusive. More...
 
static uint32 InteractiveRandom ()
 
static uint32 InteractiveRandomRange (uint32 limit)
 
static bool Chance16I (const uint a, const uint b, const uint32 r)
 Checks if a given randomize-number is below a given probability. More...
 
static bool Chance16 (const uint a, const uint b)
 Flips a coin with given probability. More...
 
static 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. More...
 

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

◆ Chance16()

static bool Chance16 ( const uint  a,
const uint  b 
)
inlinestatic

Flips a coin with given probability.

This function returns true with (a/b) probability.

See also
Chance16I()
Parameters
aThe nominator of the fraction
bThe denominator of the fraction
Returns
True with (a/b) probability

Definition at line 133 of file random_func.hpp.

References Chance16I().

Referenced by DisasterTick_Submarine(), FindSubsidyCargoDestination(), IsRoadAllowedHere(), and SetupFarmFieldFence().

◆ Chance16I()

static bool Chance16I ( const uint  a,
const uint  b,
const uint32  r 
)
inlinestatic

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
aThe numerator of the fraction
bThe denominator of the fraction, must of course not be null
rThe given randomize-number
Returns
True if the probability given by r is less or equal to (a/b)

Definition at line 114 of file random_func.hpp.

Referenced by Chance16().

◆ Chance16R()

static bool Chance16R ( const uint  a,
const uint  b,
uint32 &  r 
)
inlinestatic

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 therefore not be passed to Chance16I. One can only send the high 16 bits to Chance16I.

See also
Chance16I()
Parameters
aThe numerator of the fraction
bThe denominator of the fraction
rThe variable to save the randomize-number from Random()
Returns
True in (a/b) percent

Definition at line 157 of file random_func.hpp.

Referenced by HandleCrashedTrain().

◆ RandomRange()

static uint32 RandomRange ( uint32  limit)
inlinestatic

Pick a random number between 0 and limit - 1, inclusive.

That means 0 can be returned and limit - 1 can be returned, but limit can not be returned.

Parameters
limitLimit for the range to be picked from.
Returns
A random number in [0,limit).

Definition at line 83 of file random_func.hpp.

References Randomizer::Next().

Referenced by DoCreateNewIndustry(), Industry::GetRandom(), Town::GetRandom(), RandomHeight(), IndustryBuildData::SetupTargetCount(), StationCargoList::Truncate(), and IndustryBuildData::TryBuildNewIndustry().

◆ RestoreRandomSeeds()

static void RestoreRandomSeeds ( const SavedRandomSeeds storage)
inlinestatic

Restores previously saved seeds.

Parameters
storageStorage where SaveRandomSeeds() stored th seeds

Definition at line 54 of file random_func.hpp.

References SetRandomSeed().

◆ SaveRandomSeeds()

static void SaveRandomSeeds ( SavedRandomSeeds storage)
inlinestatic

Saves the current seeds.

Parameters
storageStorage for saving

Definition at line 44 of file random_func.hpp.

References _interactive_random, and _random.

◆ SetRandomSeed()

void SetRandomSeed ( uint32  seed)

(Re)set the state of the random number generators.

Parameters
seedthe new state

Definition at line 67 of file random_func.cpp.

References _network_server, _networking, and Randomizer::SetSeed().

Referenced by RestoreRandomSeeds().