liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
rand.h
Go to the documentation of this file.
1
6
7#ifndef LIBA_RAND_H
8#define LIBA_RAND_H
9
10#include "a.h"
11
17
18/* clang-format off */
19#define A_RAND_LCG48_INIT {A_U64_C(0x5DEECE66D), A_U16_C(0xB), {A_U16_C(0x330E), A_U16_C(0xABCD), A_U16_C(0x1234)}}
20#define A_RAND_PCG32_INIT {A_U64_C(0x5851F42D4C957F2D), A_U64_C(0xDA3E39CB94B95BDB), A_U64_C(0x853C49E6748FEA9B)}
21/* clang-format on */
22
23typedef struct a_rand_lcg48 a_rand_lcg48;
24typedef struct a_rand_pcg32 a_rand_pcg32;
25
26#if defined(__cplusplus)
27namespace a
28{
29typedef struct a_rand_lcg48 rand_lcg48;
30typedef struct a_rand_pcg32 rand_pcg32;
31} /* namespace a */
32extern "C" {
33#endif /* __cplusplus */
34#if !defined A_HAVE_INLINE || defined(LIBA_RAND_C)
36#undef A_INTERN
37#define A_INTERN A_PUBLIC extern
39#endif /* A_HAVE_INLINE */
40
46A_EXTERN A_NONULL((1)) void a_rand_lcg48_init(a_rand_lcg48 *ctx, a_i64 x);
52A_EXTERN A_NONULL((1)) void a_rand_lcg48_seed(a_rand_lcg48 *ctx, a_u16 const x[3]);
59A_EXTERN A_NONULL((1, 2)) a_u64 a_rand_lcg48_next(a_rand_lcg48 *ctx, a_u16 x[3]);
60
66A_INTERN A_NONULL((1)) a_i32 a_rand_lcg48i(a_rand_lcg48 *ctx);
73A_EXTERN A_NONULL((1, 2)) a_i32 a_rand_lcg48i_(a_rand_lcg48 *ctx, a_u16 x[3]);
79A_INTERN A_NONULL((1)) a_u32 a_rand_lcg48u(a_rand_lcg48 *ctx);
86A_EXTERN A_NONULL((1, 2)) a_u32 a_rand_lcg48u_(a_rand_lcg48 *ctx, a_u16 x[3]);
92A_INTERN A_NONULL((1)) a_f64 a_rand_lcg48f(a_rand_lcg48 *ctx);
99A_EXTERN A_NONULL((1, 2)) a_f64 a_rand_lcg48f_(a_rand_lcg48 *ctx, a_u16 x[3]);
100
108A_EXTERN A_NONULL((1)) void a_rand_lcg48_shuf(a_rand_lcg48 *ctx, void *ptr, a_size num, a_size siz);
109
116A_EXTERN A_NONULL((1)) void a_rand_pcg32_init(a_rand_pcg32 *ctx, a_u64 x, a_u64 c);
117
123A_EXTERN A_NONULL((1)) a_u32 a_rand_pcg32u(a_rand_pcg32 *ctx);
124
131A_EXTERN A_NONULL((1)) a_u32 a_rand_pcg32n(a_rand_pcg32 *ctx, a_u32 n);
132
138A_EXTERN A_NONULL((1)) a_f64 a_rand_pcg32f(a_rand_pcg32 *ctx);
139
147A_EXTERN A_NONULL((1)) void a_rand_pcg32_shuf(a_rand_pcg32 *ctx, void *ptr, a_size num, a_size siz);
148
149#if !defined A_HAVE_INLINE || defined(LIBA_RAND_C)
151#undef A_INTERN
152#define A_INTERN static A_INLINE
154#endif /* A_HAVE_INLINE */
155#if defined(__cplusplus)
156} /* extern "C" */
157#endif /* __cplusplus */
158
163{
166 a_u16 x[3];
167#if defined(__cplusplus)
169 A_INLINE void init(a_i64 x_) { a_rand_lcg48_init(this, x_); }
171 A_INLINE void seed(a_u16 const x_[3] = A_NULL) { a_rand_lcg48_seed(this, x_); }
173 A_INLINE A_NONULL((2)) a_u64 next(a_u16 x_[3]) { return a_rand_lcg48_next(this, x_); }
175 A_INLINE a_i32 i(a_u16 x_[3] = A_NULL)
176 {
177 if (x_) { return a_rand_lcg48i_(this, x_); }
178 return a_rand_lcg48i_(this, x);
179 }
180
181 A_INLINE a_u32 u(a_u16 x_[3] = A_NULL)
182 {
183 if (x_) { return a_rand_lcg48u_(this, x_); }
184 return a_rand_lcg48u_(this, x);
185 }
186
187 A_INLINE a_f64 f(a_u16 x_[3] = A_NULL)
188 {
189 if (x_) { return a_rand_lcg48f_(this, x_); }
190 return a_rand_lcg48f_(this, x);
191 }
192
193 A_INLINE void shuf(void *ptr, a_size num, a_size siz)
194 {
195 a_rand_lcg48_shuf(this, ptr, num, siz);
196 }
197#endif /* __cplusplus */
198};
199
204{
208#if defined(__cplusplus)
209#endif /* __cplusplus */
210};
211
212#if defined(LIBA_RAND_C)
214#undef A_INTERN
215#define A_INTERN A_INLINE
217#endif /* LIBA_RAND_C */
218#if defined(A_HAVE_INLINE) || defined(LIBA_RAND_C)
219
220A_INTERN a_i32 a_rand_lcg48i(a_rand_lcg48 *ctx) { return a_rand_lcg48i_(ctx, ctx->x); }
221A_INTERN a_u32 a_rand_lcg48u(a_rand_lcg48 *ctx) { return a_rand_lcg48u_(ctx, ctx->x); }
222A_INTERN a_f64 a_rand_lcg48f(a_rand_lcg48 *ctx) { return a_rand_lcg48f_(ctx, ctx->x); }
223
224#endif /* A_HAVE_INLINE */
225#if defined(LIBA_RAND_C)
227#undef A_INTERN
228#define A_INTERN static A_INLINE
230#endif /* LIBA_RAND_C */
231
233
234#endif /* a/rand.h */
algorithm library
a_f64 a_rand_lcg48f(a_rand_lcg48 *ctx)
generate a 64-bit floating-point random number in [0,1).
a_f64 a_rand_lcg48f_(a_rand_lcg48 *ctx, a_u16 x[3])
generate a 64-bit floating-point random number in [0,1).
a_f64 a_rand_pcg32f(a_rand_pcg32 *ctx)
generate a 64-bit floating-point random number in [0,1).
void a_rand_lcg48_init(a_rand_lcg48 *ctx, a_i64 x)
initialize with a seed value for the linear congruential generator and 48-bit integer arithmetic.
a_u64 a_rand_lcg48_next(a_rand_lcg48 *ctx, a_u16 x[3])
perform one iteration for the linear congruential generator and 48-bit integer arithmetic.
void a_rand_pcg32_shuf(a_rand_pcg32 *ctx, void *ptr, a_size num, a_size siz)
shuffle the elements of an array in place using the Fisher-Yates (Knuth) shuffle algorithm.
a_i32 a_rand_lcg48i_(a_rand_lcg48 *ctx, a_u16 x[3])
generate a signed 32-bit random integer.
a_u32 a_rand_pcg32n(a_rand_pcg32 *ctx, a_u32 n)
generate an unsigned 32-bit random integer in [0,n).
a_u32 a_rand_pcg32u(a_rand_pcg32 *ctx)
generate an unsigned 32-bit random integer.
a_u32 a_rand_lcg48u_(a_rand_lcg48 *ctx, a_u16 x[3])
generate an unsigned 32-bit random integer.
a_i32 a_rand_lcg48i(a_rand_lcg48 *ctx)
generate a signed 32-bit random integer.
a_u32 a_rand_lcg48u(a_rand_lcg48 *ctx)
generate an unsigned 32-bit random integer.
void a_rand_pcg32_init(a_rand_pcg32 *ctx, a_u64 x, a_u64 c)
initialize with initial value for the permuted congruential generator and 64-bit integer arithmetic.
void a_rand_lcg48_seed(a_rand_lcg48 *ctx, a_u16 const x[3])
initialize with a specific state for the linear congruential generator and 48-bit integer arithmetic.
void a_rand_lcg48_shuf(a_rand_lcg48 *ctx, void *ptr, a_size num, a_size siz)
shuffle the elements of an array in place using the Fisher-Yates (Knuth) shuffle algorithm.
unsigned long a_u64
unsigned integer type with width of exactly 64 bits
Definition a.h:627
long a_i64
signed integer type with width of exactly 64 bits
Definition a.h:589
unsigned short a_u16
unsigned integer type with width of exactly 16 bits
Definition a.h:486
double a_f64
double precision floating point type. Matches IEEE-754 binary64 format if supported.
Definition a.h:901
long a_i32
signed integer type with width of exactly 32 bits
Definition a.h:519
unsigned long a_u32
unsigned integer type with width of exactly 32 bits
Definition a.h:538
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
instance structure for the linear congruential generator and 48-bit integer arithmetic
Definition rand.h:163
void init(a_i64 x_)
initialize with a seed value for the linear congruential generator and 48-bit integer arithmetic.
Definition rand.h:169
a_u64 next(a_u16 x_[3])
perform one iteration for the linear congruential generator and 48-bit integer arithmetic.
Definition rand.h:173
a_i32 i(a_u16 x_[3]=NULL)
generate a signed 32-bit random integer.
Definition rand.h:175
void shuf(void *ptr, a_size num, a_size siz)
shuffle the elements of an array in place using the Fisher-Yates (Knuth) shuffle algorithm.
Definition rand.h:193
a_u64 a
Definition rand.h:164
a_f64 f(a_u16 x_[3]=NULL)
generate a 64-bit floating-point random number in [0,1).
Definition rand.h:187
a_u16 x[3]
Definition rand.h:166
a_u32 u(a_u16 x_[3]=NULL)
generate an unsigned 32-bit random integer.
Definition rand.h:181
void seed(a_u16 const x_[3]=NULL)
initialize with a specific state for the linear congruential generator and 48-bit integer arithmetic.
Definition rand.h:171
a_u16 c
Definition rand.h:165
instance structure for the permuted congruential generator and 64-bit integer arithmetic
Definition rand.h:204
a_u64 x
Definition rand.h:207
a_u64 a
Definition rand.h:205
a_u64 c
Definition rand.h:206