Browse Source

Add random::get() instead of extern global, which crashes Rack when

used from plugins on Windows.
tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
d4feea8210
2 changed files with 13 additions and 6 deletions
  1. +8
    -6
      include/random.hpp
  2. +5
    -0
      src/random.cpp

+ 8
- 6
include/random.hpp View File

@@ -88,24 +88,26 @@ struct Xoroshiro128Plus {

// Easy random API

extern thread_local Xoroshiro128Plus rng;


/** Initializes the thread-local RNG state.
Must call when creating a thread, otherwise random state is undefined (might always return 0).
*/
void init();

/** Returns the thread-local generator.
*/
Xoroshiro128Plus& get();

/** Returns a uniform random uint64_t from 0 to UINT64_MAX */
inline uint64_t u64() {
return rng.u64();
return get().u64();
}
/** Returns a uniform random uint32_t from 0 to UINT32_MAX */
inline uint32_t u32() {
return rng.u32();
return get().u32();
}
/** Returns a uniform random float in the interval [0.0, 1.0) */
inline float uniform() {
return rng.f32();
return get().f32();
}
/** Returns a normal random number with mean 0 and standard deviation 1 */
float normal();


+ 5
- 0
src/random.cpp View File

@@ -30,6 +30,11 @@ void init() {
}


Xoroshiro128Plus& get() {
return rng;
}


float normal() {
// Box-Muller transform
float radius = std::sqrt(-2.f * std::log(1.f - uniform()));


Loading…
Cancel
Save