Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
Random Class Reference

Project-wide source of randomness, usable from both C++ and QML. More...

#include <random.h>

Inheritance diagram for Random:
Collaboration diagram for Random:

Public Member Functions

float range (const float end) const
 Random float in the half-open interval [0.00, end).
int rangeInclusive (const int start, const int end) const
 Random integer in the closed interval [start, end].
int rangeExclusive (const int start, const int end) const
 Random integer in the half-open interval [start, end).
bool chanceFailure (const int percent) const
 Did a percent chance fail?
bool chanceFailure (const float percent) const
 Did a percent chance fail?
bool chanceSuccess (const int percent) const
 Did a percent chance succeed?
bool chanceSuccess (const float percent) const
 Did a percent chance succeed?
bool flipCoin () const
 50/50 coin flip via the integer path (chanceSuccess(50)).
bool flipCoinF () const
 50/50 coin flip via the float path (chanceSuccess(0.5f)).
void qmlProtect (const QQmlEngine *const engine) const
 Pin this object to C++ ownership so QML's GC never deletes it.

Static Public Member Functions

static Randominst ()
 < Convenience 50% coin flip (integer path), readable from QML.

Detailed Description

Project-wide source of randomness, usable from both C++ and QML.

A thin, intention-revealing wrapper around Qt's global QRandomGenerator. It exists so the rest of the app (especially the map- and full-randomization features) can express randomness in domain terms - "give me a range", "flip a coin", "did this 30% chance succeed?" - instead of repeating bounds arithmetic everywhere.

Singleton
Constructed once via inst(); the constructor is private. It is a QObject so it can be handed to QML, where it is registered as an uncreatable type and reached through Utility (pseCommon.random) rather than instantiated.
int vs float variants
Each chance/range idea comes in an integer (0-100) and a floating (0.00-1.00) flavour. The integer forms read naturally but can bias on small ranges; the float forms are offered for when they distribute better. Pick whichever yields nicer results for a given use.
Note
Does not own the underlying generator - it borrows QRandomGenerator::global().
See also
Utility, which exposes this object to QML and protects it from GC.

Definition at line 49 of file random.h.

Member Function Documentation

◆ chanceFailure() [1/2]

bool Random::chanceFailure ( const float percent) const

Did a percent chance fail?

(float scale, 0.00-1.00).

Parameters
percentSuccess likelihood as a fraction.
Returns
true when the roll lands outside the success band.

Definition at line 68 of file random.cpp.

References range().

◆ chanceFailure() [2/2]

bool Random::chanceFailure ( const int percent) const

Did a percent chance fail?

(integer scale, 0-100).

Parameters
percentSuccess likelihood as a whole percentage.
Returns
true when the roll lands outside the success band.

Definition at line 62 of file random.cpp.

References rangeInclusive().

◆ chanceSuccess() [1/2]

bool Random::chanceSuccess ( const float percent) const

Did a percent chance succeed?

(float scale, 0.00-1.00).

Parameters
percentSuccess likelihood as a fraction.
Returns
true when the roll lands inside the success band.

Definition at line 79 of file random.cpp.

References range().

◆ chanceSuccess() [2/2]

bool Random::chanceSuccess ( const int percent) const

Did a percent chance succeed?

(integer scale, 0-100).

Parameters
percentSuccess likelihood as a whole percentage.
Returns
true when the roll lands inside the success band.

Definition at line 73 of file random.cpp.

References rangeInclusive().

Referenced by flipCoin(), flipCoinF(), PokemonBox::newPokemon(), PlayerPokedex::randomize(), PokemonBox::randomize(), WorldOther::randomize(), WorldTowns::randomize(), and ItemStorageBox::randomizeBag().

◆ flipCoin()

bool Random::flipCoin ( ) const

50/50 coin flip via the integer path (chanceSuccess(50)).

Definition at line 84 of file random.cpp.

References chanceSuccess().

Referenced by PokemonBox::newPokemon(), PokemonBox::randomize(), and AreaPokemon::setTo().

◆ flipCoinF()

bool Random::flipCoinF ( ) const

50/50 coin flip via the float path (chanceSuccess(0.5f)).

Definition at line 89 of file random.cpp.

References chanceSuccess().

◆ inst()

Random * Random::inst ( )
static

< Convenience 50% coin flip (integer path), readable from QML.

Convenience 50% coin flip (float path), readable from QML. Returns the process-wide Random singleton, constructing it on first call.

Definition at line 31 of file random.cpp.

Referenced by PokemonBox::changeOtData(), PokemonBox::evolve(), ItemStorageBox::itemNew(), AreaTileset::loadFromData(), PokemonBox::newPokemon(), PokemonBox::newPokemon(), MapSearch::pickRandom(), Utility::qmlProtect(), SpriteFacing::random(), SpriteGrass::random(), SpriteMobility::random(), SpriteMovement::random(), Utility::random(), StarterPokemonDB::random3Starter(), StarterPokemonDB::randomAnyStarter(), AbstractRandomString::randomExample(), Area::randomize(), AreaAudio::randomize(), AreaGeneral::randomize(), AreaLoadedSprites::randomize(), AreaPlayer::randomize(), AreaPokemon::randomize(), AreaPokemonWild::randomize(), AreaTileset::randomize(), AreaWarps::randomize(), Daycare::randomize(), HallOfFame::randomize(), HoFPokemon::randomize(), HoFRecord::randomize(), Item::randomize(), PlayerBasics::randomize(), PlayerPokedex::randomize(), PlayerPokemon::randomize(), PokemonBox::randomize(), PokemonMove::randomize(), PokemonStorageBox::randomize(), Rival::randomize(), SignData::randomize(), SpriteData::randomize(), WarpData::randomize(), WorldOther::randomize(), WorldTowns::randomize(), ItemStorageBox::randomizeBag(), PlayerBasics::randomizeCoins(), PlayerBasics::randomizeID(), PlayerBasics::randomizeMoney(), WorldOther::randomizePlaytime(), PlayerBasics::randomizeStarter(), ItemStorageBox::randomizeStorage(), ItemStorageBox::randomUniqueInd(), PokemonBox::reRollDVs(), PokemonBox::reRollEVs(), PokemonBox::rollShiny(), Area::setTo(), AreaPokemon::setTo(), and AreaWarps::setTo().

◆ qmlProtect()

void Random::qmlProtect ( const QQmlEngine *const engine) const

Pin this object to C++ ownership so QML's GC never deletes it.

Parameters
engineThe QML engine that would otherwise assume ownership.
See also
Utility::qmlProtectUtil

Definition at line 94 of file random.cpp.

References Utility::qmlProtectUtil().

Referenced by Utility::qmlProtect().

◆ range()

float Random::range ( const float end) const

Random float in the half-open interval [0.00, end).

Parameters
endExclusive upper bound.
Returns
A value >= 0.0 and < end.

Definition at line 37 of file random.cpp.

Referenced by chanceFailure(), and chanceSuccess().

◆ rangeExclusive()

int Random::rangeExclusive ( const int start,
const int end ) const

◆ rangeInclusive()


The documentation for this class was generated from the following files: