OpenTTD Source: src/core/random_func.hpp File Reference

OpenTTD Source 15.0-RC2

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 constexpr uint32_t | ScaleToLimit (uint32_t value, uint32_t limit)
| Scale a uint32_t number to be within the range [0,limit).
void | SaveRandomSeeds (SavedRandomSeeds *storage)
| Saves the current seeds.
void | RestoreRandomSeeds (const SavedRandomSeeds &storage)
| Restores previously saved seeds.
void | SetRandomSeed (uint32_t seed)
| (Re)set the state of the random number generators.
uint32_t | Random (const std::source_location location=std::source_location::current())
uint32_t | RandomRange (uint32_t limit, const std::source_location location=std::source_location::current())
| Pick a random number between 0 and limit - 1, inclusive.
uint32_t | InteractiveRandom ()
uint32_t | InteractiveRandomRange (uint32_t limit)
bool | Chance16I (const uint32_t a, const uint32_t b, const uint32_t r)
| Checks if a given randomize-number is below a given probability.
bool | Chance16 (const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
| Flips a coin with given probability.
bool | Chance16R (const uint32_t a, const uint32_t b, uint32_t &r, const std::source_location location=std::source_location::current())
| Flips a coin with a given probability and saves the randomize-number in a variable.
void | RandomBytesWithFallback (std::span< uint8_t > buf)
| Fill the given buffer with random bytes.

## Variables
Randomizer | _random
| Random used in the game state calculations.
Randomizer | _interactive_random
| Random used everywhere else, where it does not (directly) influence the game state.

## Detailed Description

Pseudo random number generator.

Definition in file random_func.hpp.

## Function Documentation

## ◆ Chance16()

bool Chance16 | ( | const uint32_t | a,
|  | const uint32_t | b,
|  | const std::source_location | location = `std::source_location::current()`
| ) |  |
| inline

Flips a coin with given probability.

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

See also
  Chance16I()

Parameters

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

Returns
  True with (a/b) probability

Definition at line 134 of file random_func.hpp.

References Chance16I(), and Random.

Referenced by BuildTownHouse(), ChangeIndustryProduction(), CheckIfTrainNeedsService(), Disaster_Helicopter_Init(), DisasterTick_Submarine(), FindSubsidyCargoDestination(), GrowTownInTile(), IsRoadAllowedHere(), MakeWetlands(), ScaleByCargoScale(), SetupFarmFieldFence(), Vehicle::ShowVisualEffect(), TileLoop_Town(), TownActionBribe(), TryMakeRiverTerminus(), and UpdateTownGrowth().

## ◆ Chance16I()

bool Chance16I | ( | const uint32_t | a,
|  | const uint32_t | b,
|  | const uint32_t | r
| ) |  |
| inline

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 118 of file random_func.hpp.

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

## ◆ Chance16R()

bool Chance16R | ( | const uint32_t | a,
|  | const uint32_t | b,
|  | uint32_t & | r,
|  | const std::source_location | location = `std::source_location::current()`
| ) |  |
| inline

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

  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 154 of file random_func.hpp.

References Chance16I(), and Random.

Referenced by AmbientSoundEffectCallback(), HandleCrashedAircraft(), and HandleCrashedTrain().

## ◆ InteractiveRandom()

uint32_t InteractiveRandom | ( |  | ) |
| inline

Definition at line 93 of file random_func.hpp.

## ◆ InteractiveRandomRange()

uint32_t InteractiveRandomRange | ( | uint32_t | limit | ) |
| inline

Definition at line 98 of file random_func.hpp.

## ◆ Random()

uint32_t Random | ( | const std::source_location | location = `std::source_location::current()` | ) |
| inline

Definition at line 75 of file random_func.hpp.

## ◆ RandomBytesWithFallback()

void RandomBytesWithFallback | ( | std::span< uint8_t > | buf | ) |

Fill the given buffer with random bytes.

This function will attempt to use a cryptographically-strong random generator, but will fall back to a weaker random generator if none is available.

In the end, the buffer will always be filled with some form of random bytes when this function returns.

Parameters

  buf | The buffer to fill with random bytes.

Definition at line 94 of file random_func.cpp.

References Debug.

Referenced by X25519SecretKey::CreateRandom(), X25519Nonce::CreateRandom(), GenerateUid(), and X25519AuthenticationHandler::SendResponse().

## ◆ RandomRange()

uint32_t RandomRange | ( | uint32_t | limit,
|  | const std::source_location | location = `std::source_location::current()`
| ) |  |
| inline

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

  limit | Limit for the range to be picked from.

Returns
  A random number in [0,limit).

Definition at line 88 of file random_func.hpp.

References Random, and ScaleToLimit().

Referenced by AnimateTile_Town(), ChangeIndustryProduction(), CmdBuildIndustry(), CmdExpandTown(), Vehicle::Crash(), CreateNewIndustry(), DisasterTick_Big_Ufo(), DisasterTick_Ufo(), DoCreateNewIndustry(), FindSubsidyIndustryCargoRoute(), FindSubsidyPassengerRoute(), FindSubsidyTownCargoRoute(), FixSlopes(), FlowRiver(), GenerateIndustries(), Industry::GetRandom(), Town::GetRandom(), CYapfFollowShipT< Types >::GetRandomTrackdir(), FlowStat::GetVia(), FlowStat::GetVia(), FlowStat::GetViaWithRestricted(), GrowTownWithBridge(), HeightMapCurves(), MakeLake(), RandomDiagDir(), RandomHeight(), LinkRefresher::RefreshStats(), IndustryBuildData::SetupTargetCount(), TestVehicleBuildProbability(), StationCargoList::Truncate(), IndustryBuildData::TryBuildNewIndustry(), and TryBuildTownHouse().

## ◆ RestoreRandomSeeds()

void RestoreRandomSeeds | ( | const SavedRandomSeeds & | storage | ) |
| inline

Restores previously saved seeds.

Parameters

  storage | Storage where SaveRandomSeeds() stored th seeds

Definition at line 65 of file random_func.hpp.

References _interactive_random, and _random.

Referenced by CmdAutoreplaceVehicle(), CmdBuildVehicle(), and StartupOneEngine().

## ◆ SaveRandomSeeds()

void SaveRandomSeeds | ( | SavedRandomSeeds * | storage | ) |
| inline

Saves the current seeds.

Parameters

  storage | Storage for saving

Definition at line 55 of file random_func.hpp.

References _interactive_random, and _random.

Referenced by CmdAutoreplaceVehicle(), CmdBuildVehicle(), and StartupOneEngine().

## ◆ ScaleToLimit()

static constexpr uint32_t ScaleToLimit | ( | uint32_t | value,
|  | uint32_t | limit
| ) |  |
| staticconstexpr

Scale a uint32_t number to be within the range [0,limit).

Parameters

  value | The value to scale.
  limit | The limit to scale to.

Returns
  The scaled value.

Definition at line 19 of file random_func.hpp.

Referenced by Randomizer::Next(), and RandomRange().

## ◆ SetRandomSeed()

void SetRandomSeed | ( | uint32_t | seed | ) |

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

Parameters

  seed | the new state

Definition at line 65 of file random_func.cpp.

References _interactive_random, _random, and Randomizer::SetSeed().

Referenced by StartupOneEngine().

## Variable Documentation

## ◆ _interactive_random

Randomizer _interactive_random
| extern

Random used everywhere else, where it does not (directly) influence the game state.

Definition at line 36 of file random_func.cpp.

Referenced by SelectCompanyManagerFaceWindow::OnClick(), RestoreRandomSeeds(), SaveRandomSeeds(), and SetRandomSeed().

## ◆ _random

Randomizer _random
| extern

Random used in the game state calculations.

Definition at line 36 of file random_func.cpp.

Referenced by _GenerateWorld(), AfterLoadGame(), DoStartupNewCompany(), ClientNetworkGameSocketHandler::GameLoop(), GenerateTowns(), RestoreRandomSeeds(), SaveRandomSeeds(), and SetRandomSeed().