ae2f_docs
LcgRand.h
Go to the documentation of this file.
1/**
2 * @file LcgRand.h
3 * @author ae2f
4 * @brief
5 * @version 0.1
6 * @date 2025-02-13
7 *
8 * @copyright Copyright (c) 2025
9 *
10 */
11
12#ifndef ae2f_Ann_LcgRand_h
13#define ae2f_Ann_LcgRand_h
14
15#include <stdint.h>
16
17#define ae2f_AnnLcgRandA 1664525
18#define ae2f_AnnLcgRandC 1013904223
19#define ae2f_AnnLcgRandM 4294967296
20
21/// @brief Generate a random number from 0 to [ae2f_AnnLcgRandM].
22/// @param seed [uint64_t] Seed for random number calculating.
23/// @return [uint64_t] Generated random number + possible next seed.
24#define ae2f_AnnLcgRand(seed) ((ae2f_AnnLcgRandA * (seed) + ae2f_AnnLcgRandC) % ae2f_AnnLcgRandM)
25
26#include <ae2f/Float.h>
27
28/// @brief
29/// Shrink the 64-bits unsigned integer to [0, 1] in float type. \n
30///
31/// @param n [uint64_t] A 64-bits unsigned integer.
32/// @return [ae2f_float_t] [0 ~ 1]
33#define ae2f_AnnLcgRandDistribute(n) (((ae2f_float_t)(n)) / ((ae2f_float_t)ae2f_AnnLcgRandM))
34
35/// @brief
36/// Generate a random number as float point from 0 to 1.
37///
38/// @param seed [uint64_t]
39/// @return [ae2f_float_t] Generated number from 0 to 1.
40///
41/// See
42/// @ref ae2f_AnnLcgRand
43/// @ref ae2f_AnnLcgRandDistribute
44#define ae2f_AnnLcgRandReal(seed) ae2f_AnnLcgRandDistribute(ae2f_AnnLcgRand(seed))
45
46#include <ae2f/Call.h>
47#include <ae2f/Cast.h>
48
49#ifndef ae2fCL_LocAsCL
50
51/// @brief Pre-generated 64-bits seed type for separation. (under the hood).
52typedef union ae2fCL_LcgRandSeed_t {
53 uint32_t u32[2];
54 uint64_t u64;
55} ae2fCL_LcgRandSeed_t;
56
57/// @brief
58/// Pre-defined global seed. \n
59/// Simply change this would change the global seed for lcgG.
60///
61/// @see ae2f_AnnLcgRandG
62/// @see ae2f_AnnLcgRandRealG
63ae2f_extern ae2f_SHAREDCALL ae2fCL_LcgRandSeed_t
65
66/// @see ae2f_AnnLcgRand
67#define ae2f_AnnLcgRandG() ae2f_AnnLcgRand(ae2f_AnnLcgRandSeed.u64)
68
69/// @see ae2f_AnnLcgRandReal
70#define ae2f_AnnLcgRandRealG() ae2f_AnnLcgRandReal(ae2f_AnnLcgRandSeed.u64)
71
72#endif
73#endif
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
Definition Cast.h:88
ae2f_SHAREDEXPORT ae2fCL_LcgRandSeed_t ae2f_AnnLcgRandSeed
Definition LcgRand.c:4
#define ae2f_AnnLcgRandReal(seed)
Generate a random number as float point from 0 to 1.
Definition LcgRand.h:44
#define ae2f_AnnLcgRandC
Definition LcgRand.h:18
#define ae2f_AnnLcgRandM
Definition LcgRand.h:19
#define ae2f_AnnLcgRandDistribute(n)
Shrink the 64-bits unsigned integer to [0, 1] in float type. .
Definition LcgRand.h:33
#define ae2f_AnnLcgRand(seed)
Generate a random number from 0 to [ae2f_AnnLcgRandM].
Definition LcgRand.h:24
#define ae2f_AnnLcgRandA
Definition LcgRand.h:17
Pre-generated 64-bits seed type for separation. (under the hood).
Definition LcgRand.h:52
uint32_t u32[2]
Definition LcgRand.h:53