Skip to content

Commit

Permalink
perfect: Optimize random number generation by using static variables
Browse files Browse the repository at this point in the history
Modified the `chooseRandom` function to declare `std::random_device` and
`std::mt19937` as static variables. This change ensures that these objects are
initialized only once, reducing the overhead of repeatedly creating them on
each function call. This optimization is expected to improve performance by
approximately 0.02%. This aligns with practices in ggevay's original Visual Basic
code, where the random generator was a static (Shared) variable.

Reference: ggevay/malom#3 (comment)
Change-Id: Ic3b461caf52f533d4bbd5dc29ada65172d5c4450
  • Loading branch information
calcitem committed Dec 25, 2023
1 parent 97d1010 commit 6b86346
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/perfect/perfect_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ int PerfectPlayer::NGMAfterMove(const GameState &s, AdvancedMove &m)
template <typename T>
T PerfectPlayer::chooseRandom(const std::vector<T> &l)
{
static std::random_device rd;
static std::mt19937 gen(rd());

if (gameOptions.getShufflingEnabled()) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, static_cast<int>(l.size() - 1));
return l[dis(gen)];
}
Expand Down

0 comments on commit 6b86346

Please sign in to comment.