random.c
Go to the documentation of this file.
1 
11 #include <GKlib.h>
12 
13 
14 /*************************************************************************/
16 /*************************************************************************/
17 GK_MKRANDOM(gk_c, size_t, char)
18 GK_MKRANDOM(gk_i, size_t, int)
19 GK_MKRANDOM(gk_f, size_t, float)
20 GK_MKRANDOM(gk_d, size_t, double)
21 GK_MKRANDOM(gk_idx, size_t, gk_idx_t)
22 GK_MKRANDOM(gk_z, size_t, ssize_t)
23 
24 
25 
26 /*************************************************************************/
29 /*************************************************************************/
30 #ifdef USE_GKRAND
31 /*
32  A C-program for MT19937-64 (2004/9/29 version).
33  Coded by Takuji Nishimura and Makoto Matsumoto.
34 
35  This is a 64-bit version of Mersenne Twister pseudorandom number
36  generator.
37 
38  Before using, initialize the state by using init_genrand64(seed)
39  or init_by_array64(init_key, key_length).
40 
41  Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
42  All rights reserved.
43 
44  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
48  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
50  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
51  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
52  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56 
57 #define NN 312
58 #define MM 156
59 #define MATRIX_A 0xB5026F5AA96619E9ULL
60 #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
61 #define LM 0x7FFFFFFFULL /* Least significant 31 bits */
62 
63 
64 /* The array for the state vector */
65 static uint64_t mt[NN];
66 /* mti==NN+1 means mt[NN] is not initialized */
67 static int mti=NN+1;
68 #endif /* USE_GKRAND */
69 
70 /* initializes mt[NN] with a seed */
72 {
73 #ifdef USE_GKRAND
74  mt[0] = seed;
75  for (mti=1; mti<NN; mti++)
76  mt[mti] = (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
77 #else
78  srand((unsigned int) seed);
79 #endif
80 }
81 
82 
83 /* generates a random number on [0, 2^64-1]-interval */
85 {
86 #ifdef USE_GKRAND
87  int i;
88  unsigned long long x;
89  static uint64_t mag01[2]={0ULL, MATRIX_A};
90 
91  if (mti >= NN) { /* generate NN words at one time */
92  /* if init_genrand64() has not been called, */
93  /* a default initial seed is used */
94  if (mti == NN+1)
95  gk_randinit(5489ULL);
96 
97  for (i=0; i<NN-MM; i++) {
98  x = (mt[i]&UM)|(mt[i+1]&LM);
99  mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
100  }
101  for (; i<NN-1; i++) {
102  x = (mt[i]&UM)|(mt[i+1]&LM);
103  mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
104  }
105  x = (mt[NN-1]&UM)|(mt[0]&LM);
106  mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
107 
108  mti = 0;
109  }
110 
111  x = mt[mti++];
112 
113  x ^= (x >> 29) & 0x5555555555555555ULL;
114  x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
115  x ^= (x << 37) & 0xFFF7EEE000000000ULL;
116  x ^= (x >> 43);
117 
118  return x & 0x7FFFFFFFFFFFFFFF;
119 #else
120  return (uint64_t)(((uint64_t) rand()) << 32 | ((uint64_t) rand()));
121 #endif
122 }
123 
124 /* generates a random number on [0, 2^32-1]-interval */
126 {
127 #ifdef USE_GKRAND
128  return (uint32_t)(gk_randint64() & 0x7FFFFFFF);
129 #else
130  return (uint32_t)rand();
131 #endif
132 }
133 
134 
ssize_t gk_idx_t
Definition: gk_types.h:22
uint64_t gk_randint64(void)
Definition: random.c:84
unsigned int uint32_t
Definition: ms_stdint.h:85
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
void gk_randinit(uint64_t seed)
Definition: random.c:71
uint32_t gk_randint32(void)
Definition: random.c:125
#define GK_MKRANDOM(FPRFX, RNGT, VALT)
Definition: gk_mkrandom.h:23
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:29