-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathmain.c
203 lines (185 loc) · 5.26 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Note: keep project includes in alphabetical order...
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "stdlib/random/base.h"
#include "stdlib/random/base/minstd.h"
// Define the LCG multiplier:
static const uint32_t A = 16807;
// Define the maximum signed 32-bit integer: 2147483647 => 0x7fffffff => 01111111111111111111111111111111
static const uint32_t MAX_INT32 = 0x7fffffff;
// Define the normalization constant:
static const double NORMALIZATION_CONSTANT = 2147483646.0; // MAX_INT32 - 1
/**
* Returns a pseudorandom integer.
*
* @private
* @param vstate PRNG state
* @return pseudorandom integer
*/
static inline uint64_t next( void *vstate ) {
// Cast the "void state" argument to a MINSTD state object:
stdlib_base_minstd_prng_state_t *obj = (stdlib_base_minstd_prng_state_t *)( vstate );
// Retrieve the current state:
uint32_t state = obj->state;
// Explicitly cast to 64-bit to handle integer overflow:
state = (A*(uint64_t)state) % MAX_INT32;
// Update the PRNG state:
obj->state = state;
// Return the generated value:
return (uint64_t)state;
}
/**
* Returns a pseudorandom double-precision floating-point number on the interval `[0,1)`.
*
* @private
* @param vstate PRNG state
* @return pseudorandom number
*/
static inline double normalized( void *vstate ) {
double state = (double)next( vstate );
return (state-1.0) / NORMALIZATION_CONSTANT;
}
/**
* MINSTD PRNG.
*
* @private
*/
static const struct BasePRNG minstd_prng = {
"minstd", // name
(uint64_t)1, // min
(uint64_t)MAX_INT32-1, // max: (2^{31}-1) - 1
0.0, // min (normalized)
(MAX_INT32-2) / NORMALIZATION_CONSTANT, // max (normalized): (MIN-1)/MAX
sizeof( stdlib_base_minstd_prng_state_t ), // state_size
&next, // next()
&normalized // normalized()
};
/**
* Returns a pointer to a dynamically allocated MINSTD PRNG.
*
* ## Notes
*
* - The user is responsible for freeing the allocated memory.
*
* @param seed PRNG seed
* @return pointer to a dynamically allocated PRNG or, if unable to allocate memory, a null pointer
*
* @example
* #include <stdlib.h>
* #include <stdio.h>
* #include <stdint.h>
* #include "stdlib/random/base.h"
* #include "stdlib/random/base/minstd.h"
*
* // Create a MINSTD PRNG:
* struct BasePRNGObject *obj = stdlib_base_minstd_prng_allocate( 12345 );
* if ( obj == NULL ) {
* fprintf( stderr, "Error allocating memory.\n" );
* exit( 1 );
* }
*
* uint64_t r = obj->prng->next( obj->state );
*
* // ...
*
* r = obj->prng->next( obj->state );
* r = obj->prng->next( obj->state );
*
* // ...
*
* // Free allocated memory:
* stdlib_base_minstd_prng_free( obj );
*/
struct BasePRNGObject * stdlib_base_minstd_prng_allocate( const int32_t seed ) {
struct BasePRNGObject *obj = malloc( sizeof( struct BasePRNGObject ) );
if ( obj == NULL ) {
return NULL;
}
stdlib_base_minstd_prng_state_t *state = malloc( sizeof( stdlib_base_minstd_prng_state_t ) );
if ( state == NULL ) {
free( obj ); // prevent memory leaks
return NULL;
}
state->seed = (uint32_t)seed;
state->state = (uint32_t)seed;
obj->prng = &minstd_prng;
obj->state = state;
return obj;
}
/**
* Frees a MINSTD PRNG's allocated memory.
*
* @param obj PRNG object
*
* @example
* #include <stdlib.h>
* #include <stdio.h>
* #include <stdint.h>
* #include "stdlib/random/base.h"
* #include "stdlib/random/base/minstd.h"
*
* // Create a MINSTD PRNG:
* struct BasePRNGObject *obj = stdlib_base_minstd_prng_allocate( 12345 );
* if ( obj == NULL ) {
* fprintf( stderr, "Error allocating memory.\n" );
* exit( 1 );
* }
*
* uint64_t r = obj->prng->next( obj->state );
*
* // ...
*
* r = obj->prng->next( obj->state );
* r = obj->prng->next( obj->state );
*
* // ...
*
* // Free allocated memory:
* stdlib_base_minstd_prng_free( obj );
*/
void stdlib_base_minstd_prng_free( struct BasePRNGObject *obj ) {
if ( obj == NULL || strcmp( obj->prng->name, "minstd" ) != 0 ) {
return;
}
free( obj->state );
free( obj );
}
/**
* Returns a MINSTD PRNG seed.
*
* ## Notes
*
* - The function returns `-1` if unable to resolve a PRNG seed and `0` otherwise.
*
* @param obj PRNG object
* @param out output address
* @return status code
*/
int8_t stdlib_base_minstd_prng_seed( const struct BasePRNGObject *obj, int32_t *out ) {
if ( obj == NULL || strcmp( obj->prng->name, "minstd" ) != 0 ) {
return -1;
}
// Retrieve the MINSTD state object:
const stdlib_base_minstd_prng_state_t *state = (stdlib_base_minstd_prng_state_t *)( obj->state );
// Set the output value:
*out = (int32_t)( state->seed );
return 0;
}