This commit is contained in:
Reece Wilson 2023-11-17 08:19:26 +00:00
parent 999f3e69ca
commit b88c61c16d
3 changed files with 16 additions and 8 deletions

View File

@ -172,7 +172,7 @@ namespace Aurora::RNG
auto uMassiveWord = NextU32(); auto uMassiveWord = NextU32();
auto uUpperBound = AuPageRoundUp<AuUInt32>(uRange, 2); auto uUpperBound = AuPageRoundUp<AuUInt32>(uRange, 2);
AuUInt32 uNext {}; AuUInt32 uNext {};
while ((uNext = (uMassiveWord & (uUpperBound - 1))) >= uRange) while ((uNext = (uMassiveWord & (uUpperBound - 1))) > uRange)
{ {
uMassiveWord = NextU32(); uMassiveWord = NextU32();
} }
@ -190,7 +190,7 @@ namespace Aurora::RNG
auto uMassiveWord = NextU32(); auto uMassiveWord = NextU32();
auto uUpperBound = AuPageRoundUp<AuUInt32>(uRange, 2); auto uUpperBound = AuPageRoundUp<AuUInt32>(uRange, 2);
AuUInt32 uNext {}; AuUInt32 uNext {};
while ((uNext = (uMassiveWord & (uUpperBound - 1))) >= uRange) while ((uNext = (uMassiveWord & (uUpperBound - 1))) > uRange)
{ {
uMassiveWord = NextU32(); uMassiveWord = NextU32();
} }

View File

@ -49,21 +49,21 @@ static auline void WELL_SeedRand(WELLRand *rand, AuUInt32 seed)
/** /**
* Creates a new random number generator from a given seed. * Creates a new random number generator from a given seed.
*/ */
WELLRand &&WELL_SeedRand(AuUInt32 seed) WELLRand WELL_SeedRand(AuUInt32 seed)
{ {
WELLRand rand {}; WELLRand rand {};
WELL_SeedRand(&rand, seed); WELL_SeedRand(&rand, seed);
return AuMove(rand); return AuMove(rand);
} }
WELLRand &&WELL_SeedRand64(AuUInt64 seed) WELLRand WELL_SeedRand64(AuUInt64 seed)
{ {
WELLRand rand {}; WELLRand rand {};
WELL_SeedRand64(&rand, seed); WELL_SeedRand64(&rand, seed);
return AuMove(rand); return AuMove(rand);
} }
WELLRand &&WELL_SeedRandBig64(const AuArray<AuUInt8, 64> &seed) WELLRand WELL_SeedRandBig64(const AuArray<AuUInt8, 64> &seed)
{ {
WELLRand rand {}; WELLRand rand {};
static_assert(64 == sizeof(rand.state)); static_assert(64 == sizeof(rand.state));

View File

@ -6,6 +6,14 @@ struct WELLRand
int index {}; int index {};
AuFutexMutex lock; AuFutexMutex lock;
inline WELLRand() = default;
inline WELLRand(WELLRand &&rand)
{
AuMemcpy(this->state, rand.state, sizeof(this->state));
this->index = rand.index;
}
inline WELLRand &operator =(WELLRand &&rand) inline WELLRand &operator =(WELLRand &&rand)
{ {
AuMemcpy(this->state, rand.state, sizeof(this->state)); AuMemcpy(this->state, rand.state, sizeof(this->state));
@ -14,8 +22,8 @@ struct WELLRand
} }
}; };
WELLRand &&WELL_SeedRand(AuUInt32 seed); WELLRand WELL_SeedRand(AuUInt32 seed);
WELLRand &&WELL_SeedRandBig64(const AuArray<AuUInt8, 64> &seed); WELLRand WELL_SeedRandBig64(const AuArray<AuUInt8, 64> &seed);
WELLRand &&WELL_SeedRand64(AuUInt64 seed); WELLRand WELL_SeedRand64(AuUInt64 seed);
AuUInt32 WELL_NextLong(WELLRand* rand); AuUInt32 WELL_NextLong(WELLRand* rand);
void WELL_NextBytes(WELLRand *rand, void *in, AuUInt32 length); void WELL_NextBytes(WELLRand *rand, void *in, AuUInt32 length);