Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
legacy_random.c File Reference
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "legacy_random.h"
+ Include dependency graph for legacy_random.c:

Go to the source code of this file.

Functions

void clear_checksum ()
 
int16_t munge_checksum (long long value_)
 
double cunningFee (double value, double precision)
 
unsigned Ranrot (void)
 
unsigned RanrotWithSeed (RANROTSeed *ioSeed)
 
RANROTSeed MakeRanrotSeed (uint32_t seed)
 
RANROTSeed RanrotSeedFromRNGSeed (RNG_Seed seed)
 
RANROTSeed RanrotSeedFromRandomSeed (Random_Seed seed)
 
void ranrot_srand (uint32_t seed)
 
float randf (void)
 
float randfWithSeed (RANROTSeed *ioSeed)
 
float bellf (int n)
 
RANROTSeed RANROTGetFullSeed (void)
 
void RANROTSetFullSeed (RANROTSeed seed)
 
void seed_RNG_only_for_planet_description (Random_Seed s_seed)
 
void seed_for_planet_description (Random_Seed s_seed)
 
RNG_Seed currentRandomSeed (void)
 
void setRandomSeed (RNG_Seed a_seed)
 
int gen_rnd_number (void)
 
uint32_t OOReallyRandom (void)
 
void OOInitReallyRandom (uint64_t seed)
 
void OOSetReallyRandomRANROTSeed (void)
 
void OOSetReallyRandomRndSeed (void)
 
void OOSetReallyRandomRANROTAndRndSeeds (void)
 
OORandomState OOSaveRandomState (void)
 
void OORestoreRandomState (OORandomState state)
 
void make_pseudo_random_seed (Random_Seed *seed_ptr)
 
void rotate_seed (Random_Seed *seed_ptr)
 

Variables

const Random_Seed kNilRandomSeed = {0}
 
static RNG_Seed rnd_seed
 
static int32_t checksum
 
static RANROTSeed sRANROT
 
static bool sReallyRandomInited = false
 
static RANROTSeed sReallyRandomSeed
 

Function Documentation

◆ bellf()

float bellf ( int n)

Definition at line 168 of file legacy_random.c.

169{
170 int i = n;
171 float total = 0;
172
173 if (EXPECT_NOT(i <= 0))
174 {
175 printf("***** ERROR - attempt to generate bellf(%d)\n", n);
176 return 0.0f; // catch possible div-by-zero problem
177 }
178
179 while (i-- > 0)
180 total += (Ranrot() & 1023);
181 return total / (1024.0f * n);
182}
#define EXPECT_NOT(x)
unsigned Ranrot(void)

References EXPECT_NOT, and Ranrot().

+ Here is the call graph for this function:

◆ clear_checksum()

void clear_checksum ( void )

Definition at line 42 of file legacy_random.c.

43{
44 checksum = 0;
45}
static int32_t checksum

References checksum.

◆ cunningFee()

double cunningFee ( double value,
double precision )

Definition at line 62 of file legacy_random.c.

63{
64 double fee = value;
65 double superfee = 100000.0;
66 double max = 1 + precision;
67 double min = 1 - precision;
68 unsigned long long rounded_fee = superfee * floor(0.5 + fee / superfee);
69 if (rounded_fee == 0) rounded_fee = 1;
70 double ratio = fee / (double)rounded_fee;
71
72 while ((ratio < min || ratio > max) && superfee > 1)
73 {
74 rounded_fee = superfee * floor(0.5 + fee / superfee);
75 if (rounded_fee == 0) rounded_fee = 1;
76 ratio = fee / (double)rounded_fee;
77 superfee /= 10.0;
78 }
79
80 if (ratio > min && ratio < max)
81 fee = rounded_fee;
82
83 return fee;
84}

◆ currentRandomSeed()

RNG_Seed currentRandomSeed ( void )

Definition at line 213 of file legacy_random.c.

214{
215 return rnd_seed;
216}
static RNG_Seed rnd_seed

References rnd_seed.

Referenced by GlobalRandomName().

+ Here is the caller graph for this function:

◆ gen_rnd_number()

int gen_rnd_number ( void )

Definition at line 225 of file legacy_random.c.

226{
227 int a,x;
228
229 x = (rnd_seed.a * 2) & 0xFF;
230 a = x + rnd_seed.c;
231 if (rnd_seed.a > 127)
232 a++;
233 rnd_seed.a = a & 0xFF;
234 rnd_seed.c = x;
235
236 a = a / 256; /* a = any carry left from above */
237 x = rnd_seed.b;
238 a = (a + x + rnd_seed.d) & 0xFF;
239 rnd_seed.b = a;
240 rnd_seed.d = x;
241 return a;
242}
float x
int32_t c
int32_t d
int32_t a
int32_t b

References RNG_Seed::a, RNG_Seed::b, RNG_Seed::c, RNG_Seed::d, rnd_seed, and x.

Referenced by make_pseudo_random_seed(), and OldRandomDigrams().

+ Here is the caller graph for this function:

◆ make_pseudo_random_seed()

void make_pseudo_random_seed ( Random_Seed * seed_ptr)

Definition at line 309 of file legacy_random.c.

310{
311 seed_ptr->a = gen_rnd_number();
312 seed_ptr->b = gen_rnd_number();
313 seed_ptr->c = gen_rnd_number();
314 seed_ptr->d = gen_rnd_number();
315 seed_ptr->e = gen_rnd_number();
316 seed_ptr->f = gen_rnd_number();
317}
int gen_rnd_number(void)

References Random_Seed::a, Random_Seed::b, Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, and gen_rnd_number().

Referenced by GlobalRandomInhabitantsDescription().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MakeRanrotSeed()

RANROTSeed MakeRanrotSeed ( uint32_t seed)

Definition at line 113 of file legacy_random.c.

114{
115 RANROTSeed result =
116 {
117 .low = seed,
118 .high = ~seed
119 };
120
121 // Mix it up a bit.
122 RanrotWithSeed(&result);
123 RanrotWithSeed(&result);
124 RanrotWithSeed(&result);
125
126 return result;
127}
unsigned RanrotWithSeed(RANROTSeed *ioSeed)

References RanrotWithSeed().

Referenced by ranrot_srand(), and RanrotSeedFromRNGSeed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ munge_checksum()

int16_t munge_checksum ( long long value_)

Definition at line 48 of file legacy_random.c.

49{
50 uint32_t value = (uint32_t)value_;
51 int32_t mult1 = (value & 15) + 8;
52 checksum += value;
53 checksum *= mult1;
54 checksum += mult1;
55 checksum &= 0xffff;
56 return checksum;
57}

References checksum.

◆ OOInitReallyRandom()

void OOInitReallyRandom ( uint64_t seed)

Definition at line 256 of file legacy_random.c.

257{
258 assert(!sReallyRandomInited);
259 seed ^= 0xA471D52AEF3B6322ULL;
260 sReallyRandomSeed.high = (seed >> 32) & 0xFFFFFFFF;
261 sReallyRandomSeed.low = seed & 0xFFFFFFFF;
262 sReallyRandomInited = true;
264}
static bool sReallyRandomInited
uint32_t OOReallyRandom(void)
static RANROTSeed sReallyRandomSeed
uint32_t low
uint32_t high

References RANROTSeed::high, RANROTSeed::low, OOReallyRandom(), sReallyRandomInited, and sReallyRandomSeed.

+ Here is the call graph for this function:

◆ OOReallyRandom()

uint32_t OOReallyRandom ( void )

Definition at line 249 of file legacy_random.c.

250{
251 assert(sReallyRandomInited);
253}

References RanrotWithSeed(), sReallyRandomInited, and sReallyRandomSeed.

Referenced by OOInitReallyRandom(), OOSetReallyRandomRANROTSeed(), and OOSetReallyRandomRndSeed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OORestoreRandomState()

void OORestoreRandomState ( OORandomState state)

Definition at line 302 of file legacy_random.c.

303{
304 sRANROT = state.ranrot;
305 rnd_seed = state.rnd;
306}
static RANROTSeed sRANROT
RANROTSeed ranrot

References OORandomState::ranrot, OORandomState::rnd, rnd_seed, and sRANROT.

Referenced by OOExpandDescriptionString().

+ Here is the caller graph for this function:

◆ OOSaveRandomState()

OORandomState OOSaveRandomState ( void )

Definition at line 292 of file legacy_random.c.

293{
294 return (OORandomState)
295 {
296 .ranrot = sRANROT,
297 .rnd = rnd_seed
298 };
299}

References rnd_seed, and sRANROT.

Referenced by OOExpandDescriptionString().

+ Here is the caller graph for this function:

◆ OOSetReallyRandomRANROTAndRndSeeds()

void OOSetReallyRandomRANROTAndRndSeeds ( void )

Definition at line 285 of file legacy_random.c.

286{
289}
void OOSetReallyRandomRANROTSeed(void)
void OOSetReallyRandomRndSeed(void)

References OOSetReallyRandomRANROTSeed(), and OOSetReallyRandomRndSeed().

Referenced by OOExpandDescriptionString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOSetReallyRandomRANROTSeed()

void OOSetReallyRandomRANROTSeed ( void )

Definition at line 267 of file legacy_random.c.

268{
269 assert(sReallyRandomInited);
271 OOReallyRandom(); // Don't go reusing it.
272}

References OOReallyRandom(), sRANROT, sReallyRandomInited, and sReallyRandomSeed.

Referenced by OOSetReallyRandomRANROTAndRndSeeds().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOSetReallyRandomRndSeed()

void OOSetReallyRandomRndSeed ( void )

Definition at line 275 of file legacy_random.c.

276{
277 uint32_t val = OOReallyRandom();
278 rnd_seed.a = (val >> 24) & 0xFF;
279 rnd_seed.b = (val >> 16) & 0xFF;
280 rnd_seed.c = (val >> 8) & 0xFF;
281 rnd_seed.d = val & 0xFF;
282}

References RNG_Seed::a, RNG_Seed::b, RNG_Seed::c, RNG_Seed::d, OOReallyRandom(), and rnd_seed.

Referenced by OOSetReallyRandomRANROTAndRndSeeds().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randf()

float randf ( void )

Definition at line 156 of file legacy_random.c.

157{
158 return (Ranrot() & 0xffff) * (1.0f / 65536.0f);
159}

References Ranrot().

Referenced by WormholeEntity::DrawWormholeCorona, fillRanNoiseBuffer(), OOFuzzyBooleanFromObject(), OOHPRandomPositionInBoundingBox(), OOHPVectorRandomRadial(), OOHPVectorRandomSpatial(), OORandomPositionInBoundingBox(), OORandomPositionInCylinder(), OORandomUnitHPVector(), OORandomUnitVector(), OOVectorRandomRadial(), and OOVectorRandomSpatial().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randfWithSeed()

float randfWithSeed ( RANROTSeed * ioSeed)

Definition at line 162 of file legacy_random.c.

163{
164 return (RanrotWithSeed(ioSeed) & 0xffff) * (1.0f / 65536.0f);
165}

References RanrotWithSeed().

+ Here is the call graph for this function:

◆ Ranrot()

unsigned Ranrot ( void )

Definition at line 93 of file legacy_random.c.

94{
95 sRANROT.high = (sRANROT.high << 16) + (sRANROT.high >> 16);
98 return sRANROT.high & 0x7FFFFFFF;
99}

References RANROTSeed::high, RANROTSeed::low, and sRANROT.

Referenced by bellf(), GlobalRandomName(), quaternion_set_random(), randf(), Octree::randomFullNodeFrom, StationAddShipToShipyard(), and SVGDumpBeginPrimitive().

+ Here is the caller graph for this function:

◆ ranrot_srand()

void ranrot_srand ( uint32_t seed)

Definition at line 150 of file legacy_random.c.

151{
152 sRANROT = MakeRanrotSeed(seed);
153}
RANROTSeed MakeRanrotSeed(uint32_t seed)

References MakeRanrotSeed(), and sRANROT.

+ Here is the call graph for this function:

◆ RANROTGetFullSeed()

RANROTSeed RANROTGetFullSeed ( void )

Definition at line 185 of file legacy_random.c.

186{
187 return sRANROT;
188}

References sRANROT.

Referenced by fillRanNoiseBuffer().

+ Here is the caller graph for this function:

◆ RanrotSeedFromRandomSeed()

RANROTSeed RanrotSeedFromRandomSeed ( Random_Seed seed)

Definition at line 136 of file legacy_random.c.

137{
138 // Same pattern as seed_for_planet_description().
139 RNG_Seed s =
140 {
141 .a = seed.c,
142 .b = seed.d,
143 .c = seed.e,
144 .d = seed.f
145 };
146 return RanrotSeedFromRNGSeed(s);
147}
RANROTSeed RanrotSeedFromRNGSeed(RNG_Seed seed)

References Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, and RanrotSeedFromRNGSeed().

+ Here is the call graph for this function:

◆ RanrotSeedFromRNGSeed()

RANROTSeed RanrotSeedFromRNGSeed ( RNG_Seed seed)

Definition at line 130 of file legacy_random.c.

131{
132 return MakeRanrotSeed(seed.a * 0x1000000 + seed.b * 0x10000 + seed.c * 0x100 + seed.d);
133}

References RNG_Seed::a, RNG_Seed::b, RNG_Seed::c, RNG_Seed::d, and MakeRanrotSeed().

Referenced by RanrotSeedFromRandomSeed(), and seed_for_planet_description().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RANROTSetFullSeed()

void RANROTSetFullSeed ( RANROTSeed seed)

Definition at line 191 of file legacy_random.c.

192{
193 sRANROT = seed;
194}

References sRANROT.

◆ RanrotWithSeed()

unsigned RanrotWithSeed ( RANROTSeed * ioSeed)

Definition at line 102 of file legacy_random.c.

103{
104 assert(ioSeed != NULL);
105
106 ioSeed->high = (ioSeed->high << 16) + (ioSeed->high >> 16);
107 ioSeed->high += ioSeed->low;
108 ioSeed->low += ioSeed->high;
109 return ioSeed->high & 0x7FFFFFFF;
110}

References RANROTSeed::high, and RANROTSeed::low.

Referenced by MakeRanrotSeed(), OOReallyRandom(), and randfWithSeed().

+ Here is the caller graph for this function:

◆ rotate_seed()

void rotate_seed ( Random_Seed * seed_ptr)

Definition at line 320 of file legacy_random.c.

321{
322 uint_fast16_t x;
323 uint_fast16_t y;
324
325 /* Note: this is equivalent to adding three (little-endian) 16-bit values
326 together, rotating the three numbers and replacing one of them with
327 the sum. The byte-oriented approach is presumably because it was
328 reverse-engineered from eight-bit machine code. Switching to a plain
329 sixteen-bit representation is more trouble than it's worth since so
330 much code uses byte values from the seed struct directly.
331 */
332 x = seed_ptr->a + seed_ptr->c + seed_ptr->e;
333 y = seed_ptr->b + seed_ptr->d + seed_ptr->f;
334
335 seed_ptr->a = seed_ptr->c;
336 seed_ptr->b = seed_ptr->d;
337
338 seed_ptr->c = seed_ptr->e;
339 seed_ptr->d = seed_ptr->f;
340
341 seed_ptr->e = x;
342 seed_ptr->f = y + (x >> 8);
343}
float y

References Random_Seed::a, Random_Seed::b, Random_Seed::c, Random_Seed::d, Random_Seed::e, Random_Seed::f, x, and y.

◆ seed_for_planet_description()

void seed_for_planet_description ( Random_Seed s_seed)

Definition at line 206 of file legacy_random.c.

207{
210}
void seed_RNG_only_for_planet_description(Random_Seed s_seed)

References RanrotSeedFromRNGSeed(), rnd_seed, seed_RNG_only_for_planet_description(), and sRANROT.

+ Here is the call graph for this function:

◆ seed_RNG_only_for_planet_description()

void seed_RNG_only_for_planet_description ( Random_Seed s_seed)

Definition at line 197 of file legacy_random.c.

198{
199 rnd_seed.a = s_seed.c;
200 rnd_seed.b = s_seed.d;
201 rnd_seed.c = s_seed.e;
202 rnd_seed.d = s_seed.f;
203}

References RNG_Seed::a, RNG_Seed::b, Random_Seed::c, RNG_Seed::c, Random_Seed::d, RNG_Seed::d, Random_Seed::e, Random_Seed::f, and rnd_seed.

Referenced by OOGenerateSystemDescription(), and seed_for_planet_description().

+ Here is the caller graph for this function:

◆ setRandomSeed()

void setRandomSeed ( RNG_Seed a_seed)

Definition at line 219 of file legacy_random.c.

220{
221 rnd_seed = a_seed;
222}

References rnd_seed.

Referenced by GlobalRandomName().

+ Here is the caller graph for this function:

Variable Documentation

◆ checksum

int32_t checksum
static

Definition at line 41 of file legacy_random.c.

Referenced by clear_checksum(), and munge_checksum().

◆ kNilRandomSeed

const Random_Seed kNilRandomSeed = {0}

◆ rnd_seed

◆ sRANROT

◆ sReallyRandomInited

bool sReallyRandomInited = false
static

◆ sReallyRandomSeed

RANROTSeed sReallyRandomSeed
static