diff options
| author | Tyge Løvset <[email protected]> | 2022-05-30 14:22:44 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-05-30 14:22:44 +0200 |
| commit | 58bb58e7980d1eae175ed66dbe873893a05ab81e (patch) | |
| tree | 2756b2d1b62264c34a6d40265dc85f07eb2f1112 /include/stc/crandom.h | |
| parent | b28d3fa7c3b9233ca485014744bf84e6c4f5a1d3 (diff) | |
| download | STC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.tar.gz STC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.zip | |
Done refactoring:
- Non-templated types (cstr, csview, cbits, crandom) have no longer default static linking. Now i_header is defined, i.e. files are as headers only.
==> Define `i_implement` before file inclusion. Still possible to do static linkage by defining `i_static` before inclusion or global STC_STATIC.
- Templated containers still have static linkage by default.
- Renamed csview_substr(), csview_slice() to csview_substr_ex(), csview_slice_ex(). Added simpler inlined csview_substr(), csview_slice().
Diffstat (limited to 'include/stc/crandom.h')
| -rw-r--r-- | include/stc/crandom.h | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/include/stc/crandom.h b/include/stc/crandom.h index a9877b50..a3f68279 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#define i_header
#include "ccommon.h"
#ifndef CRANDOM_H_INCLUDED
@@ -73,6 +74,10 @@ STC_INLINE stc64_t stc64_new(uint64_t seed) STC_API stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high);
STC_API int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist);
+/* Normal distribution PRNG */
+STC_API double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist);
+
+
/* Main stc64 prng */
STC_INLINE uint64_t stc64_rand(stc64_t* rng) {
uint64_t *s = rng->state; enum {LR=24, RS=11, LS=3};
@@ -104,21 +109,6 @@ STC_INLINE stc64_normalf_t stc64_normalf_new(double mean, double stddev) { return c_make(stc64_normalf_t){mean, stddev, 0.0, 0};
}
-/* Normal distribution PRNG */
-STC_INLINE double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
- double u1, u2, s, m;
- if (dist->has_next++ & 1)
- return dist->next * dist->stddev + dist->mean;
- do {
- u1 = 2.0 * stc64_randf(rng) - 1.0;
- u2 = 2.0 * stc64_randf(rng) - 1.0;
- s = u1*u1 + u2*u2;
- } while (s >= 1.0 || s == 0.0);
- m = sqrt(-2.0 * log(s) / s);
- dist->next = u2 * m;
- return (u1 * m) * dist->stddev + dist->mean;
-}
-
/* Following functions are deprecated (will be removed in the future): */
STC_INLINE void stc64_srandom(uint64_t seed) { csrandom(seed); }
STC_INLINE uint64_t stc64_random() { return crandom(); }
@@ -181,6 +171,21 @@ STC_DEF int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) { #endif
}
+/* Normal distribution PRNG */
+STC_DEF double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
+ double u1, u2, s, m;
+ if (dist->has_next++ & 1)
+ return dist->next * dist->stddev + dist->mean;
+ do {
+ u1 = 2.0 * stc64_randf(rng) - 1.0;
+ u2 = 2.0 * stc64_randf(rng) - 1.0;
+ s = u1*u1 + u2*u2;
+ } while (s >= 1.0 || s == 0.0);
+ m = sqrt(-2.0 * log(s) / s);
+ dist->next = u2 * m;
+ return (u1 * m) * dist->stddev + dist->mean;
+}
+
#endif
#endif
#undef i_opt
|
