Skip to content

Commit

Permalink
ofRandom & al: typos, format, wording (#7691)
Browse files Browse the repository at this point in the history
  • Loading branch information
artificiel committed Sep 29, 2023
1 parent 2de89ae commit 1b90592
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions apps/devApps/RandomExplorer/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void ofApp::draw() {
auto x = 220;
ofDrawBitmapStringHighlight(size_string_, x, 40);
ofDrawBitmapStringHighlight(dna_string_, x, 80);
ofDrawBitmapStringHighlight("<- these values will be deterministic for a given seed (on all platforms)\n<- otherwise always different for non-deterministic", x+400, 80, ofColor(40,40,40));
ofDrawBitmapStringHighlight("<- these values will be deterministic for a given seed (on all platforms)\n<- otherwise always different for non-deterministic", x + 400, 80, ofColor(40, 40, 40));
ofDrawBitmapStringHighlight(shuffle_string_, x, 100);
ofDrawBitmapStringHighlight("other random functions (like std::shuffle) can feed from the same thread-safe engine, ensuring coherence", x, 140);

Expand All @@ -86,6 +86,6 @@ void ofApp::draw() {
dists_["of"]->draw("OF/art-centric wrappers/utils", square_, gap_);
dists_["old"]->draw("Previous implementation (reference)", square_, gap_);

ofDrawBitmapStringHighlight("Performance: e.g. on macOS M1,the old srand is faster\nthan uniform<float> in Debug, but slower in Release...\nPlease make sure to evaluate performance in Release!",
ofDrawBitmapStringHighlight("Performance: on M1/M2 processors, the old srand is faster\nthan uniform<float> in Debug, but slower in Release...\nPlease make sure to evaluate performance in Release!",
dists_["old"]->panel_.getPosition() + glm::vec2(0, square_ + gap_), ofColor(50, 0, 0), ofColor(ofColor::white));
}
3 changes: 2 additions & 1 deletion apps/devApps/RandomExplorer/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ class ofApp : public ofBaseApp {
auto colorize() {

size_t sum = 0;
for (auto & group : { dists_["core"], dists_["special"], dists_["of"] })
for (auto & group : { dists_["core"], dists_["special"], dists_["of"] }) {
sum += group->dists_.size();
}
auto chunk = 255.0 / sum;

size_t i = 0; // to spread the hue; order below matters
Expand Down
18 changes: 11 additions & 7 deletions libs/openFrameworks/utils/ofRandomEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,41 @@ class Engine : public of::utils::Singleton<Engine> {
std::random_device rd_ {};
std::seed_seq seq_ { rd_(), rd_(), rd_(), rd_() }; // 4 is considered fine for non-cryptographic needs
std::mt19937 gen_ { seq_ };
bool deterministic_ { false }; // by default the degine is non-deterministic (unpredictable)
bool deterministic_ { false }; // by default the engine is non-deterministic (unpredictable)

public:
/// return the generator for use in random distributions or functions
///
/// \brief return the generator for use in random distributions or functions
/// \returns a reference to the mt19937 generator
auto & gen() { return gen_; }

/// passes a value to seed the mt19937 generator
/// \brief seeds the the mt19937 generator
/// \param new_seed the seed value
void seed(unsigned long new_seed) {
deterministic_ = true;
gen_.seed(new_seed);
}

/// \brief return the generator for use in random distributions or functions
/// \returns true or fall depending if the engine has been seeded with a deterministic value
auto is_deterministic() const { return deterministic_; }
};

/// \brief retrieve the engine instance
/// \returns a reference to the engine instance
inline auto engine() {
return of::random::Engine::instance();
}

/// \brief retrieve the generator instance
/// \returns a reference to the generator within the engine instance
inline auto & gen() {
return of::random::Engine::instance()->gen();
}

/// Passes a value to seed the mt19937 generator within the engine instance
inline void seed(unsigned long seed) {
of::random::Engine::instance()->seed(seed);
/// \brief seeds the the mt19937 generator
/// \param new_seed the seed value
inline void seed(unsigned long new_seed) {
of::random::Engine::instance()->seed(new_seed);
}

} // end namespace of::random
Expand Down
5 changes: 4 additions & 1 deletion libs/openFrameworks/utils/ofSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ namespace of::utils {
template <typename Derived>
class Singleton {
public:
/// \brief constructs the instance
template <typename... Args>
static void construct(Args &&... args) {
struct Dummy : public Derived {
using Derived::Derived;
void prohibit_construct_from_derived() const override { }
};

using Instance = Dummy;
if (!instance_.load(std::memory_order_acquire)) {
std::lock_guard lock { mutex_ };
Expand All @@ -30,6 +31,7 @@ class Singleton {
}
}

/// \brief destroys the instance
static void destruct() {
if (instance_.load(std::memory_order_acquire)) {
std::lock_guard lock { mutex_ };
Expand All @@ -40,6 +42,7 @@ class Singleton {
}
}

/// \brief returns the instance
static Derived * instance() {
auto * the_instance = instance_.load(std::memory_order_acquire);
if (!the_instance) {
Expand Down

0 comments on commit 1b90592

Please sign in to comment.