Random.h
Go to the documentation of this file.
1 #ifndef MiscLib__RANDOM_HEADER__
2 #define MiscLib__RANDOM_HEADER__
3 /*
4  * random.h -- Random number generation interface
5  *
6  * for further details see Knuth TAOCP Vol.2 pp. 186f
7  *
8  */
9 
10 #define MiscLib_RN_BUFSIZE 500
11 #define MiscLib_RN_RAND_MOD (1L << 30)
12 #define MiscLib_RN_CONST 6180339887L
13 
14 namespace MiscLib
15 {
16  extern size_t rn_buf[];
17  extern size_t rn_point;
18  void rn_setseed(size_t);
19  size_t rn_refresh(void);
20  inline size_t rn_rand()
21  {
22  size_t idx = rn_point++;
23  return (MiscLib_RN_BUFSIZE > idx) ?
24  rn_buf[idx] : rn_refresh();
25  }
26  inline size_t rn_urand(size_t m)
27  {
28  return rn_rand() % m;
29  }
30  inline float rn_frand()
31  {
32  return (float)rn_rand() / MiscLib_RN_RAND_MOD;
33  }
34 };
35 
36 #endif
MiscLib_RN_BUFSIZE
#define MiscLib_RN_BUFSIZE
Definition: Random.h:10
MiscLib::rn_point
size_t rn_point
Definition: Random.cpp:23
MiscLib_RN_RAND_MOD
#define MiscLib_RN_RAND_MOD
Definition: Random.h:11
MiscLib::rn_frand
float rn_frand()
Definition: Random.h:30
MiscLib::rn_setseed
void rn_setseed(size_t)
Definition: Random.cpp:25
MiscLib::rn_rand
size_t rn_rand()
Definition: Random.h:20
MiscLib
Definition: AlignedAllocator.h:11
MiscLib::rn_buf
size_t rn_buf[]
Definition: Random.cpp:22
MiscLib::rn_refresh
size_t rn_refresh(void)
Definition: Random.cpp:92
MiscLib::rn_urand
size_t rn_urand(size_t m)
Definition: Random.h:26