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/* clang-format on */
21
22typedef struct a_rand_lcg48 a_rand_lcg48;
23
24#if defined(__cplusplus)
25namespace a
26{
27typedef struct a_rand_lcg48 rand_lcg48;
28} /* namespace a */
29extern "C" {
30#endif /* __cplusplus */
31#if !defined A_HAVE_INLINE || defined(LIBA_RAND_C)
33#undef A_INTERN
34#define A_INTERN A_PUBLIC extern
36#endif /* A_HAVE_INLINE */
37
43A_EXTERN A_NONULL((1)) void a_rand_lcg48_init(a_rand_lcg48 *ctx, a_i64 x);
49A_EXTERN A_NONULL((1)) void a_rand_lcg48_seed(a_rand_lcg48 *ctx, a_u16 const x[3]);
56A_EXTERN A_NONULL((1, 2)) a_u64 a_rand_lcg48_next(a_rand_lcg48 *ctx, a_u16 x[3]);
57
63A_INTERN A_NONULL((1)) a_i32 a_rand_lcg48i(a_rand_lcg48 *ctx);
70A_EXTERN A_NONULL((1, 2)) a_i32 a_rand_lcg48i_(a_rand_lcg48 *ctx, a_u16 x[3]);
76A_INTERN A_NONULL((1)) a_u32 a_rand_lcg48u(a_rand_lcg48 *ctx);
83A_EXTERN A_NONULL((1, 2)) a_u32 a_rand_lcg48u_(a_rand_lcg48 *ctx, a_u16 x[3]);
89A_INTERN A_NONULL((1)) a_f64 a_rand_lcg48f(a_rand_lcg48 *ctx);
96A_EXTERN A_NONULL((1, 2)) a_f64 a_rand_lcg48f_(a_rand_lcg48 *ctx, a_u16 x[3]);
97
105A_EXTERN A_NONULL((1)) void a_rand_lcg48_shuf(a_rand_lcg48 *ctx, void *ptr, a_size num, a_size siz);
106
107#if !defined A_HAVE_INLINE || defined(LIBA_RAND_C)
109#undef A_INTERN
110#define A_INTERN static A_INLINE
112#endif /* A_HAVE_INLINE */
113#if defined(__cplusplus)
114} /* extern "C" */
115#endif /* __cplusplus */
116
121{
124 a_u16 x[3];
125#if defined(__cplusplus)
127 A_INLINE void init(a_i64 x_) { a_rand_lcg48_init(this, x_); }
129 A_INLINE void seed(a_u16 const x_[3] = A_NULL) { a_rand_lcg48_seed(this, x_); }
131 A_INLINE A_NONULL((2)) a_u64 next(a_u16 x_[3]) { return a_rand_lcg48_next(this, x_); }
133 A_INLINE a_i32 i(a_u16 x_[3] = A_NULL)
134 {
135 if (x_) { return a_rand_lcg48i_(this, x_); }
136 return a_rand_lcg48i_(this, x);
137 }
138
139 A_INLINE a_u32 u(a_u16 x_[3] = A_NULL)
140 {
141 if (x_) { return a_rand_lcg48u_(this, x_); }
142 return a_rand_lcg48u_(this, x);
143 }
144
145 A_INLINE a_f64 f(a_u16 x_[3] = A_NULL)
146 {
147 if (x_) { return a_rand_lcg48f_(this, x_); }
148 return a_rand_lcg48f_(this, x);
149 }
150
151 A_INLINE void shuf(void *ptr, a_size num, a_size siz)
152 {
153 a_rand_lcg48_shuf(this, ptr, num, siz);
154 }
155#endif /* __cplusplus */
156};
157
158#if defined(LIBA_RAND_C)
160#undef A_INTERN
161#define A_INTERN A_INLINE
163#endif /* LIBA_RAND_C */
164#if defined(A_HAVE_INLINE) || defined(LIBA_RAND_C)
165
166A_INTERN a_i32 a_rand_lcg48i(a_rand_lcg48 *ctx) { return a_rand_lcg48i_(ctx, ctx->x); }
167A_INTERN a_u32 a_rand_lcg48u(a_rand_lcg48 *ctx) { return a_rand_lcg48u_(ctx, ctx->x); }
168A_INTERN a_f64 a_rand_lcg48f(a_rand_lcg48 *ctx) { return a_rand_lcg48f_(ctx, ctx->x); }
169
170#endif /* A_HAVE_INLINE */
171#if defined(LIBA_RAND_C)
173#undef A_INTERN
174#define A_INTERN static A_INLINE
176#endif /* LIBA_RAND_C */
177
179
180#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).
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.
a_i32 a_rand_lcg48i_(a_rand_lcg48 *ctx, a_u16 x[3])
generate a signed 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_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:121
void init(a_i64 x_)
initialize with a seed value for the linear congruential generator and 48-bit integer arithmetic.
Definition rand.h:127
a_u64 next(a_u16 x_[3])
perform one iteration for the linear congruential generator and 48-bit integer arithmetic.
Definition rand.h:131
a_i32 i(a_u16 x_[3]=NULL)
generate a signed 32-bit random integer.
Definition rand.h:133
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:151
a_u64 a
Definition rand.h:122
a_f64 f(a_u16 x_[3]=NULL)
generate a 64-bit floating-point random number in [0,1).
Definition rand.h:145
a_u16 x[3]
Definition rand.h:124
a_u32 u(a_u16 x_[3]=NULL)
generate an unsigned 32-bit random integer.
Definition rand.h:139
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:129
a_u16 c
Definition rand.h:123