kerep-headers
This commit is contained in:
72
kerep-headers/random/krandom.h
Normal file
72
kerep-headers/random/krandom.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/std.h"
|
||||
#include "splitmix64/splitmix64.h"
|
||||
#include "xoroshiro/xoroshiro.h"
|
||||
#include "xoshiro/xoshiro.h"
|
||||
|
||||
/*
|
||||
You can choose any algorithm that has required functions:
|
||||
|
||||
some_alg32_statePtr some_alg32_init(u32 seed);
|
||||
u32 some_alg32_next(some_alg32_statePtr);
|
||||
void some_alg32_free(some_alg32_statePtr);
|
||||
|
||||
#define KRAND_ALG32_init some_alg32_init
|
||||
#define KRAND_ALG32_next some_alg32_next
|
||||
#define KRAND_ALG32_free some_alg32_free
|
||||
#include "kerep/random/krandom.h"
|
||||
|
||||
The same way it works for 64-bit RNGs
|
||||
*/
|
||||
|
||||
// default rng_next function
|
||||
#ifndef KRAND_ALG32_next
|
||||
#define KRAND_ALG32_next xoshiro128plus##_next
|
||||
#endif
|
||||
#ifndef KRAND_ALG32_init
|
||||
#define KRAND_ALG32_init xoshiro128plus##_init
|
||||
#endif
|
||||
#ifndef KRAND_ALG32_free
|
||||
#define KRAND_ALG32_free xoshiro128plus##_free
|
||||
#endif
|
||||
#ifndef KRAND_ALG64_next
|
||||
#define KRAND_ALG64_next xoshiro256plus##_next
|
||||
#endif
|
||||
#ifndef KRAND_ALG64_init
|
||||
#define KRAND_ALG64_init xoshiro256plus##_init
|
||||
#endif
|
||||
#ifndef KRAND_ALG64_free
|
||||
#define KRAND_ALG64_free xoshiro256plus##_free
|
||||
#endif
|
||||
|
||||
typedef void* krand_statePtr;
|
||||
#define KRAND_ALG32_initFromTime xoshiro128plus##_initFromTime
|
||||
#define KRAND_ALG64_initFromTime xoshiro256plus##_initFromTime
|
||||
|
||||
#define __krand_next_definition(VALUE_SIZE) { return from+KRAND_ALG##VALUE_SIZE##_next(state)%(to-from); }
|
||||
|
||||
// ready-to-use functions
|
||||
static inline i8 krand_next8 (krand_statePtr state, i8 from, i8 to) __krand_next_definition(32)
|
||||
static inline i16 krand_next16(krand_statePtr state, i16 from, i16 to) __krand_next_definition(32)
|
||||
static inline i32 krand_next32(krand_statePtr state, i32 from, i32 to) __krand_next_definition(32)
|
||||
static inline i64 krand_next64(krand_statePtr state, i64 from, i64 to) __krand_next_definition(64)
|
||||
|
||||
// divides random number by 2^64 to return a value between 0 and 1
|
||||
static inline f32 krand_nextFloat32(krand_statePtr state) {return (u32)KRAND_ALG32_next(state)/0xffffffff; }
|
||||
static inline f64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; }
|
||||
|
||||
|
||||
///@param chance (0-1.0) is probability of success
|
||||
static inline bool fate(krand_statePtr state,float chance){
|
||||
i32 limit=1/chance + 0.01f;
|
||||
return KRAND_ALG32_next(state)%limit == 0;
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
22
kerep-headers/random/splitmix64/splitmix64.h
Normal file
22
kerep-headers/random/splitmix64/splitmix64.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../base/base.h"
|
||||
|
||||
typedef u64 splitmix64_state;
|
||||
typedef void* splitmix64_statePtr;
|
||||
|
||||
splitmix64_statePtr splitmix64_init(u64 seed);
|
||||
static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); }
|
||||
|
||||
u64 splitmix64_next(splitmix64_statePtr);
|
||||
static inline void splitmix64_free(splitmix64_statePtr state) {
|
||||
free(state);
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
35
kerep-headers/random/xoroshiro/32bitValue/xoroshiro64.h
Normal file
35
kerep-headers/random/xoroshiro/32bitValue/xoroshiro64.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../../base/std.h"
|
||||
#include "../../splitmix64/splitmix64.h"
|
||||
|
||||
typedef union {
|
||||
u64 merged;
|
||||
u32 s[2];
|
||||
} xoroshiro64_state;
|
||||
typedef void* xoroshiro64_statePtr;
|
||||
|
||||
xoroshiro64_statePtr xoroshiro64_init(u64 seed);
|
||||
#define xoroshiro64star_init xoroshiro64_init
|
||||
#define xoroshiro64starstar_init xoroshiro64_init
|
||||
|
||||
static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoroshiro64_init(time(NULL)); }
|
||||
#define xoroshiro64star_initFromTime xoroshiro64_initFromTime
|
||||
#define xoroshiro64starstar_initFromTime xoroshiro64_initFromTime
|
||||
|
||||
u32 xoroshiro64star_next(xoroshiro64_statePtr);
|
||||
u32 xoroshiro64starstar_next(xoroshiro64_statePtr);
|
||||
|
||||
static inline void xoroshiro64_free(xoroshiro64_statePtr state) {
|
||||
free(state);
|
||||
}
|
||||
#define xoroshiro64star_free xoroshiro64_free
|
||||
#define xoroshiro64starstar_free xoroshiro64_free
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
39
kerep-headers/random/xoroshiro/64bitValue/xoroshiro128.h
Normal file
39
kerep-headers/random/xoroshiro/64bitValue/xoroshiro128.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../../base/std.h"
|
||||
#include "../../splitmix64/splitmix64.h"
|
||||
|
||||
|
||||
typedef union {
|
||||
u32 s[2];
|
||||
} xoroshiro128_state;
|
||||
typedef void* xoroshiro128_statePtr;
|
||||
|
||||
xoroshiro128_statePtr xoroshiro128_init(u64 seed);
|
||||
#define xoroshiro128plus_init xoroshiro128_init
|
||||
#define xoroshiro128plusplus_init xoroshiro128_init
|
||||
#define xoroshiro128starstar_init xoroshiro128_init
|
||||
|
||||
static inline xoroshiro128_statePtr xoroshiro128_initFromTime(void) { return xoroshiro128_init(time(NULL)); }
|
||||
#define xoroshiro128plus_initFromTime xoroshiro128_initFromTime
|
||||
#define xoroshiro128plusplus_initFromTime xoroshiro128_initFromTime
|
||||
#define xoroshiro128starstar_initFromTime xoroshiro128_initFromTime
|
||||
|
||||
u64 xoroshiro128plus_next(xoroshiro128_statePtr);
|
||||
u64 xoroshiro128plusplus_next(xoroshiro128_statePtr);
|
||||
u64 xoroshiro128starstar_next(xoroshiro128_statePtr);
|
||||
|
||||
static inline void xoroshiro128_free(xoroshiro128_statePtr state) {
|
||||
free(state);
|
||||
}
|
||||
#define xoroshiro128plus_free xoroshiro128_free
|
||||
#define xoroshiro128plusplus_free xoroshiro128_free
|
||||
#define xoroshiro128starstar_free xoroshiro128_free
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
2
kerep-headers/random/xoroshiro/xoroshiro.h
Normal file
2
kerep-headers/random/xoroshiro/xoroshiro.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "32bitValue/xoroshiro64.h"
|
||||
#include "64bitValue/xoroshiro128.h"
|
||||
24
kerep-headers/random/xoshiro-xoroshiro.md
Normal file
24
kerep-headers/random/xoshiro-xoroshiro.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Xoshiro/Xoroshiro RNG algorithms
|
||||
There are a bunch of versions of xoshiro/xoroshiro algorithms, which are created by [David Blackman and Sebastiano Vigna](https://prng.di.unimi.it/)
|
||||
|
||||
|
||||
```
|
||||
xoroshiro
|
||||
├── 32bitValue
|
||||
| ├── xoroshiro64star.c
|
||||
| └── xoroshiro64starstar.c
|
||||
└── 64bitValue
|
||||
├── xoroshiro128plus.c
|
||||
├── xoroshiro128plusplus.c
|
||||
└── xoroshiro128starstar.c
|
||||
|
||||
xoshiro
|
||||
├── 32bitValue
|
||||
│ ├── xoshiro128plus.c
|
||||
│ ├── xoshiro128plusplus.c
|
||||
│ └── xoshiro128starstar.c
|
||||
└── 64bitValue
|
||||
├── xoshiro256plus.c
|
||||
├── xoshiro256plusplus.c
|
||||
└── xoshiro256starstar.c
|
||||
```
|
||||
40
kerep-headers/random/xoshiro/32bitValue/xoshiro128.h
Normal file
40
kerep-headers/random/xoshiro/32bitValue/xoshiro128.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../../base/std.h"
|
||||
#include "../../splitmix64/splitmix64.h"
|
||||
|
||||
|
||||
typedef union {
|
||||
u64 merged[2];
|
||||
u32 s[4];
|
||||
} xoshiro128_state;
|
||||
typedef void* xoshiro128_statePtr;
|
||||
|
||||
xoshiro128_statePtr xoshiro128_init(u64 seed);
|
||||
#define xoshiro128plus_init xoshiro128_init
|
||||
#define xoshiro128plusplus_init xoshiro128_init
|
||||
#define xoshiro128starstar_init xoshiro128_init
|
||||
|
||||
static inline xoshiro128_statePtr xoshiro128_initFromTime(void) { return xoshiro128_init(time(NULL)); }
|
||||
#define xoshiro128plus_initFromTime xoshiro128_initFromTime
|
||||
#define xoshiro128plusplus_initFromTime xoshiro128_initFromTime
|
||||
#define xoshiro128starstar_initFromTime xoshiro128_initFromTime
|
||||
|
||||
u32 xoshiro128plus_next(xoshiro128_statePtr);
|
||||
u32 xoshiro128plusplus_next(xoshiro128_statePtr);
|
||||
u32 xoshiro128starstar_next(xoshiro128_statePtr);
|
||||
|
||||
static inline void xoshiro128_free(xoshiro128_statePtr state) {
|
||||
free(state);
|
||||
}
|
||||
#define xoshiro128plus_free xoshiro128_free
|
||||
#define xoshiro128plusplus_free xoshiro128_free
|
||||
#define xoshiro128starstar_free xoshiro128_free
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
39
kerep-headers/random/xoshiro/64bitValue/xoshiro256.h
Normal file
39
kerep-headers/random/xoshiro/64bitValue/xoshiro256.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../../../base/std.h"
|
||||
#include "../../splitmix64/splitmix64.h"
|
||||
|
||||
|
||||
typedef union {
|
||||
u64 s[4];
|
||||
} xoshiro256_state;
|
||||
typedef void* xoshiro256_statePtr;
|
||||
|
||||
xoshiro256_statePtr xoshiro256_init(u64 seed);
|
||||
#define xoshiro256plus_init xoshiro256_init
|
||||
#define xoshiro256plusplus_init xoshiro256_init
|
||||
#define xoshiro256starstar_init xoshiro256_init
|
||||
|
||||
static inline xoshiro256_statePtr xoshiro256_initFromTime(void) { return xoshiro256_init(time(NULL)); }
|
||||
#define xoshiro256plus_initFromTime xoshiro256_initFromTime
|
||||
#define xoshiro256plusplus_initFromTime xoshiro256_initFromTime
|
||||
#define xoshiro256starstar_initFromTime xoshiro256_initFromTime
|
||||
|
||||
u64 xoshiro256plus_next(xoshiro256_statePtr);
|
||||
u64 xoshiro256plusplus_next(xoshiro256_statePtr);
|
||||
u64 xoshiro256starstar_next(xoshiro256_statePtr);
|
||||
|
||||
static inline void xoshiro256_free(xoshiro256_statePtr state) {
|
||||
free(state);
|
||||
}
|
||||
#define xoshiro256plus_free xoshiro256_free
|
||||
#define xoshiro256plusplus_free xoshiro256_free
|
||||
#define xoshiro256starstar_free xoshiro256_free
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
2
kerep-headers/random/xoshiro/xoshiro.h
Normal file
2
kerep-headers/random/xoshiro/xoshiro.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "32bitValue/xoshiro128.h"
|
||||
#include "64bitValue/xoshiro256.h"
|
||||
Reference in New Issue
Block a user