-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace RandomGenerator singleton with global instance
- Remove singleton pattern (static get(), deleted constructors) - Add global pointer instance gRandomGenerator - Rename members to follow camelCase convention
- Loading branch information
Showing
2 changed files
with
25 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,27 @@ | ||
#pragma once | ||
#include <random> | ||
#include <memory> | ||
|
||
class CRandomGenerator { | ||
public: | ||
static CRandomGenerator& get() { | ||
static CRandomGenerator instance; | ||
return instance; | ||
} | ||
|
||
CRandomGenerator() : mGenerator(mRandomDevice()) {} | ||
|
||
// Generate random index for vector size | ||
size_t getRandomIndex(size_t size) { | ||
if (size == 0) return 0; | ||
std::uniform_int_distribution<size_t> dis(0, size - 1); | ||
return dis(m_Generator); | ||
std::uniform_int_distribution<size_t> distribution(0, size - 1); | ||
return distribution(mGenerator); | ||
} | ||
|
||
// Get raw generator if needed | ||
std::mt19937& getGenerator() { | ||
return m_Generator; | ||
return mGenerator; | ||
} | ||
|
||
private: | ||
CRandomGenerator() : m_Generator(m_RandomDevice()) {} | ||
|
||
std::random_device m_RandomDevice; | ||
std::mt19937 m_Generator; | ||
std::random_device mRandomDevice; | ||
std::mt19937 mGenerator; | ||
}; | ||
|
||
// Delete copy/move to ensure singleton | ||
CRandomGenerator(const CRandomGenerator&) = delete; | ||
CRandomGenerator& operator=(const CRandomGenerator&) = delete; | ||
CRandomGenerator(CRandomGenerator&&) = delete; | ||
CRandomGenerator& operator=(CRandomGenerator&&) = delete; | ||
}; | ||
// Global pointer to random generator | ||
inline std::unique_ptr<CRandomGenerator> gRandomGenerator; |