Got tired of the confusion about random number generators and added OOReallyRandom(), which is guaranteed not to be used for any sort of repeatable value anywhere, and is seeded exactly once.
git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@5416 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
parent
76347c282b
commit
f4d8901bbe
@ -256,6 +256,8 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
|
||||
self = [super init];
|
||||
if (self == nil) return nil;
|
||||
|
||||
OOInitReallyRandom([NSDate timeIntervalSinceReferenceDate] * 1e9);
|
||||
|
||||
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
strict = [prefs oo_boolForKey:@"strict-gameplay" defaultValue:NO];
|
||||
|
@ -26,6 +26,7 @@ MA 02110-1301, USA.
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include "legacy_random.h"
|
||||
|
||||
@ -224,14 +225,14 @@ void setRandomSeed (RNG_Seed a_seed)
|
||||
int gen_rnd_number (void)
|
||||
{
|
||||
int a,x;
|
||||
|
||||
|
||||
x = (rnd_seed.a * 2) & 0xFF;
|
||||
a = x + rnd_seed.c;
|
||||
if (rnd_seed.a > 127)
|
||||
a++;
|
||||
rnd_seed.a = a & 0xFF;
|
||||
rnd_seed.c = x;
|
||||
|
||||
|
||||
a = a / 256; /* a = any carry left from above */
|
||||
x = rnd_seed.b;
|
||||
a = (a + x + rnd_seed.d) & 0xFF;
|
||||
@ -241,6 +242,70 @@ int gen_rnd_number (void)
|
||||
}
|
||||
|
||||
|
||||
static bool sReallyRandomInited = false;
|
||||
static RANROTSeed sReallyRandomSeed;
|
||||
|
||||
|
||||
uint32_t OOReallyRandom(void)
|
||||
{
|
||||
assert(sReallyRandomInited);
|
||||
return RanrotWithSeed(&sReallyRandomSeed);
|
||||
}
|
||||
|
||||
|
||||
void OOInitReallyRandom(uint64_t seed)
|
||||
{
|
||||
assert(!sReallyRandomInited);
|
||||
seed ^= 0xA471D52AEF3B6322ULL;
|
||||
sReallyRandomSeed.high = (seed >> 32) & 0xFFFFFFFF;
|
||||
sReallyRandomSeed.low = seed & 0xFFFFFFFF;
|
||||
sReallyRandomInited = true;
|
||||
OOReallyRandom();
|
||||
}
|
||||
|
||||
|
||||
void OOSetReallyRandomRANROTSeed(void)
|
||||
{
|
||||
assert(sReallyRandomInited);
|
||||
sRANROT = sReallyRandomSeed;
|
||||
OOReallyRandom(); // Don't go reusing it.
|
||||
}
|
||||
|
||||
|
||||
void OOSetReallyRandomRndSeed(void)
|
||||
{
|
||||
uint32_t val = OOReallyRandom();
|
||||
rnd_seed.a = (val >> 24) & 0xFF;
|
||||
rnd_seed.b = (val >> 16) & 0xFF;
|
||||
rnd_seed.c = (val >> 8) & 0xFF;
|
||||
rnd_seed.d = val & 0xFF;
|
||||
}
|
||||
|
||||
|
||||
void OOSetReallyRandomRANROTAndRndSeeds(void)
|
||||
{
|
||||
OOSetReallyRandomRANROTSeed();
|
||||
OOSetReallyRandomRndSeed();
|
||||
}
|
||||
|
||||
|
||||
OORandomState OOSaveRandomState(void)
|
||||
{
|
||||
return (OORandomState)
|
||||
{
|
||||
.ranrot = sRANROT,
|
||||
.rnd = rnd_seed
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void OORestoreRandomState(OORandomState state)
|
||||
{
|
||||
sRANROT = state.ranrot;
|
||||
rnd_seed = state.rnd;
|
||||
}
|
||||
|
||||
|
||||
void make_pseudo_random_seed (Random_Seed *seed_ptr)
|
||||
{
|
||||
seed_ptr->a = gen_rnd_number();
|
||||
|
@ -31,6 +31,7 @@ MA 02110-1301, USA.
|
||||
|
||||
#include "OOFunctionAttributes.h"
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct Random_Seed
|
||||
@ -110,6 +111,37 @@ OOINLINE int rotate_byte_left (int x) INLINE_CONST_FUNC;
|
||||
OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2) INLINE_CONST_FUNC;
|
||||
|
||||
|
||||
/*
|
||||
The "really really random" PRNG. This is a separate RANROT seed that is
|
||||
seeded once at startup and never reset under any circumstances. It can
|
||||
also be used to seed get_rnd_number and the main RANROT seed. If doing this,
|
||||
save and restore the seeds using the functions above ore OOSaveRandomState()
|
||||
and OORestoreRandomState().
|
||||
|
||||
Since these use a global seed, they may only be used from the main thread.
|
||||
*/
|
||||
|
||||
uint32_t OOReallyRandom(void);
|
||||
void OOInitReallyRandom(uint64_t seed);
|
||||
|
||||
void OOSetReallyRandomRANROTSeed(void);
|
||||
void OOSetReallyRandomRndSeed(void);
|
||||
void OOSetReallyRandomRANROTAndRndSeeds(void);
|
||||
|
||||
/*
|
||||
OOSaveRandomState()/OORestoreRandomState(): save and restore both the main
|
||||
RANROT seed and the rnd seed in one shot.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
RANROTSeed ranrot;
|
||||
RNG_Seed rnd;
|
||||
} OORandomState;
|
||||
|
||||
OORandomState OOSaveRandomState(void);
|
||||
void OORestoreRandomState(OORandomState state);
|
||||
|
||||
|
||||
|
||||
/*** Only inline definitions beyond this point ***/
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user