Oolite
Loading...
Searching...
No Matches
legacy_random.c
Go to the documentation of this file.
1/*
2
3legacy_random.c
4
5
6Oolite
7Copyright (C) 2004-2008 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#include "legacy_random.h"
27#include <assert.h>
28#include <math.h>
29#include <stdbool.h>
30#include <stdint.h>
31#include <stdio.h>
32
34
36
37// TODO: Why is this based on a static? Should change to OOMungeCheckSum(&checkSum, value);
38static int32_t checksum;
40{
41 checksum = 0;
42}
43
44int16_t munge_checksum(long long value_)
45{
46 uint32_t value = (uint32_t)value_;
47 int32_t mult1 = (value & 15) + 8;
48 checksum += value;
49 checksum *= mult1;
50 checksum += mult1;
51 checksum &= 0xffff;
52 return checksum;
53}
54
55// cunning price rounding routine:
56//
57double cunningFee(double value, double precision)
58{
59 double fee = value;
60 double superfee = 100000.0;
61 double max = 1 + precision;
62 double min = 1 - precision;
63 unsigned long long rounded_fee = superfee * floor(0.5 + fee / superfee);
64 if (rounded_fee == 0)
65 rounded_fee = 1;
66 double ratio = fee / (double)rounded_fee;
67
68 while ((ratio < min || ratio > max) && superfee > 1) {
69 rounded_fee = superfee * floor(0.5 + fee / superfee);
70 if (rounded_fee == 0)
71 rounded_fee = 1;
72 ratio = fee / (double)rounded_fee;
73 superfee /= 10.0;
74 }
75
76 if (ratio > min && ratio < max)
77 fee = rounded_fee;
78
79 return fee;
80}
81
82// an implementation of RANROT
83// pseudo random number generator
84//
86
87unsigned Ranrot(void)
88{
89 sRANROT.high = (sRANROT.high << 16) + (sRANROT.high >> 16);
90 sRANROT.high += sRANROT.low;
91 sRANROT.low += sRANROT.high;
92 return sRANROT.high & 0x7FFFFFFF;
93}
94
95unsigned RanrotWithSeed(RANROTSeed* ioSeed)
96{
97 assert(ioSeed != NULL);
98
99 ioSeed->high = (ioSeed->high << 16) + (ioSeed->high >> 16);
100 ioSeed->high += ioSeed->low;
101 ioSeed->low += ioSeed->high;
102 return ioSeed->high & 0x7FFFFFFF;
103}
104
106{
107 RANROTSeed result = {
108 .low = seed,
109 .high = ~seed
110 };
111
112 // Mix it up a bit.
113 RanrotWithSeed(&result);
114 RanrotWithSeed(&result);
115 RanrotWithSeed(&result);
116
117 return result;
118}
119
121{
122 return MakeRanrotSeed(seed.a * 0x1000000 + seed.b * 0x10000 + seed.c * 0x100 + seed.d);
123}
124
126{
127 // Same pattern as seed_for_planet_description().
128 RNG_Seed s = {
129 .a = seed.c,
130 .b = seed.d,
131 .c = seed.e,
132 .d = seed.f
133 };
134 return RanrotSeedFromRNGSeed(s);
135}
136
137void ranrot_srand(uint32_t seed)
138{
139 sRANROT = MakeRanrotSeed(seed);
140}
141
142float randf(void)
143{
144 return (Ranrot() & 0xffff) * (1.0f / 65536.0f);
145}
146
148{
149 return (RanrotWithSeed(ioSeed) & 0xffff) * (1.0f / 65536.0f);
150}
151
152float bellf(int n)
153{
154 int i = n;
155 float total = 0;
156
157 if (EXPECT_NOT(i <= 0)) {
158 printf("***** ERROR - attempt to generate bellf(%d)\n", n);
159 return 0.0f; // catch possible div-by-zero problem
160 }
161
162 while (i-- > 0)
163 total += (Ranrot() & 1023);
164 return total / (1024.0f * n);
165}
166
168{
169 return sRANROT;
170}
171
173{
174 sRANROT = seed;
175}
176
178{
179 rnd_seed.a = s_seed.c;
180 rnd_seed.b = s_seed.d;
181 rnd_seed.c = s_seed.e;
182 rnd_seed.d = s_seed.f;
183}
184
190
192{
193 return rnd_seed;
194}
195
197{
198 rnd_seed = a_seed;
199}
200
202{
203 int a, x;
204
205 x = (rnd_seed.a * 2) & 0xFF;
206 a = x + rnd_seed.c;
207 if (rnd_seed.a > 127)
208 a++;
209 rnd_seed.a = a & 0xFF;
210 rnd_seed.c = x;
211
212 a = a / 256; /* a = any carry left from above */
213 x = rnd_seed.b;
214 a = (a + x + rnd_seed.d) & 0xFF;
215 rnd_seed.b = a;
216 rnd_seed.d = x;
217 return a;
218}
219
220#ifndef NDEBUG
221// Only read by assert(), so builds without assertions leave it out.
222static bool sReallyRandomInited = false;
223#endif
225
226uint32_t OOReallyRandom(void)
227{
228 assert(sReallyRandomInited);
230}
231
232void OOInitReallyRandom(uint64_t seed)
233{
234 assert(!sReallyRandomInited);
235 seed ^= 0xA471D52AEF3B6322ULL;
236 sReallyRandomSeed.high = (seed >> 32) & 0xFFFFFFFF;
237 sReallyRandomSeed.low = seed & 0xFFFFFFFF;
238#ifndef NDEBUG
239 sReallyRandomInited = true;
240#endif
242}
243
245{
246 assert(sReallyRandomInited);
248 OOReallyRandom(); // Don't go reusing it.
249}
250
252{
253 uint32_t val = OOReallyRandom();
254 rnd_seed.a = (val >> 24) & 0xFF;
255 rnd_seed.b = (val >> 16) & 0xFF;
256 rnd_seed.c = (val >> 8) & 0xFF;
257 rnd_seed.d = val & 0xFF;
258}
259
265
267{
268 return (OORandomState) {
269 .ranrot = sRANROT,
270 .rnd = rnd_seed
271 };
272}
273
275{
276 sRANROT = state.ranrot;
277 rnd_seed = state.rnd;
278}
279
281{
282 seed_ptr->a = gen_rnd_number();
283 seed_ptr->b = gen_rnd_number();
284 seed_ptr->c = gen_rnd_number();
285 seed_ptr->d = gen_rnd_number();
286 seed_ptr->e = gen_rnd_number();
287 seed_ptr->f = gen_rnd_number();
288}
289
291{
292 uint16_t x;
293 uint16_t y;
294
295 /* Note: this is equivalent to adding three (little-endian) 16-bit values
296 together, rotating the three numbers and replacing one of them with
297 the sum. The byte-oriented approach is presumably because it was
298 reverse-engineered from eight-bit machine code. Switching to a plain
299 sixteen-bit representation is more trouble than it's worth since so
300 much code uses byte values from the seed struct directly.
301 */
302 x = seed_ptr->a + seed_ptr->c + seed_ptr->e;
303 y = seed_ptr->b + seed_ptr->d + seed_ptr->f;
304
305 seed_ptr->a = seed_ptr->c;
306 seed_ptr->b = seed_ptr->d;
307
308 seed_ptr->c = seed_ptr->e;
309 seed_ptr->d = seed_ptr->f;
310
311 seed_ptr->e = x;
312 seed_ptr->f = y + (x >> 8);
313}
#define EXPECT_NOT(x)
float y
float x
RANROTSeed RanrotSeedFromRandomSeed(Random_Seed seed)
const Random_Seed kNilRandomSeed
float randf(void)
void clear_checksum()
void make_pseudo_random_seed(Random_Seed *seed_ptr)
static bool sReallyRandomInited
RANROTSeed RANROTGetFullSeed(void)
void seed_RNG_only_for_planet_description(Random_Seed s_seed)
void ranrot_srand(uint32_t seed)
void OOSetReallyRandomRANROTSeed(void)
void setRandomSeed(RNG_Seed a_seed)
int16_t munge_checksum(long long value_)
uint32_t OOReallyRandom(void)
float randfWithSeed(RANROTSeed *ioSeed)
float bellf(int n)
RNG_Seed currentRandomSeed(void)
void OOInitReallyRandom(uint64_t seed)
static RANROTSeed sRANROT
void OORestoreRandomState(OORandomState state)
static RANROTSeed sReallyRandomSeed
OORandomState OOSaveRandomState(void)
void RANROTSetFullSeed(RANROTSeed seed)
RANROTSeed RanrotSeedFromRNGSeed(RNG_Seed seed)
static int32_t checksum
void OOSetReallyRandomRndSeed(void)
unsigned RanrotWithSeed(RANROTSeed *ioSeed)
int gen_rnd_number(void)
double cunningFee(double value, double precision)
static RNG_Seed rnd_seed
void seed_for_planet_description(Random_Seed s_seed)
RANROTSeed MakeRanrotSeed(uint32_t seed)
void OOSetReallyRandomRANROTAndRndSeeds(void)
unsigned Ranrot(void)
void rotate_seed(Random_Seed *seed_ptr)
RANROTSeed ranrot
uint32_t low
uint32_t high
int32_t c
int32_t d
int32_t a
int32_t b