OpenTTD Source  20241125-master-g5b02f51e17
random_func.cpp File Reference

Implementation of the pseudo random generator. More...

#include "../stdafx.h"
#include "random_func.hpp"
#include "bitmath_func.hpp"
#include "../debug.h"
#include "../safeguards.h"

Go to the source code of this file.

Functions

void SetRandomSeed (uint32_t seed)
 (Re)set the state of the random number generators. More...
 
void RandomBytesWithFallback (std::span< uint8_t > buf)
 Fill the given buffer with random bytes. More...
 

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

Implementation of the pseudo random generator.

Definition in file random_func.cpp.

Function Documentation

◆ 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
bufThe buffer to fill with random bytes.

Definition at line 95 of file random_func.cpp.

References Debug.

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

◆ SetRandomSeed()

void SetRandomSeed ( uint32_t  seed)

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

Parameters
seedthe new state

Definition at line 66 of file random_func.cpp.

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

Referenced by StartupOneEngine().