[+] IRandomDevice::NextArrayI32Range
[+] IRandomDevice::NextArrayU32Range [+] IRandomDevice::NextArrayDoubleRange [+] IRandomDevice::NextArrayI32 [+] IRandomDevice::NextArrayU32 [+] IRandomDevice::NextArrayDouble [+] IRandomDevice::NextArrayDecimals [+] IRandomDevice::NextArrayUUIDs [*] IRandomDevice minor refactoring for consistency [*] Improve decimals once again (perf/deterministic)
This commit is contained in:
parent
c64cb0c0e2
commit
c16a0b1600
@ -20,13 +20,21 @@ namespace Aurora::RNG
|
||||
virtual AuUInt8 NextByte() = 0;
|
||||
virtual bool NextBoolean() = 0;
|
||||
virtual AuUInt32 NextU32() = 0;
|
||||
virtual AuUInt32 NextU32(AuUInt32 uMin, AuUInt32 uMax) = 0;
|
||||
virtual AuUInt32 NextU32Range(AuUInt32 uMin, AuUInt32 uMax) = 0;
|
||||
virtual AuUInt64 NextU64() = 0;
|
||||
virtual AuInt32 NextInt(AuInt32 iMin, AuInt32 iMax) = 0;
|
||||
virtual AuInt32 NextI32Range(AuInt32 iMin, AuInt32 iMax) = 0;
|
||||
virtual double NextDecimal() = 0;
|
||||
virtual double NextNumber(double dMin, double dMax) = 0;
|
||||
virtual AuUInt32 NextIndex(AuUInt32 uCount /* = max + 1*/) = 0;
|
||||
virtual uuids::uuid NextUUID() = 0;
|
||||
virtual AuList<AuInt32> NextArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax) = 0;
|
||||
virtual AuList<AuUInt32> NextArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax) = 0;
|
||||
virtual AuList<double> NextArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax) = 0;
|
||||
virtual AuList<AuInt32> NextArrayI32(AuUInt32 uCount) = 0;
|
||||
virtual AuList<AuUInt32> NextArrayU32(AuUInt32 uCount) = 0;
|
||||
virtual AuList<double> NextArrayDouble(AuUInt32 uCount) = 0;
|
||||
virtual AuList<double> NextArrayDecimals(AuUInt32 uCount) = 0;
|
||||
virtual AuList<uuids::uuid> NextArrayUUIDs(AuUInt32 uCount) = 0;
|
||||
|
||||
virtual Memory::MemoryViewRead ToSeed() = 0;
|
||||
|
||||
|
@ -22,14 +22,21 @@ namespace Aurora::RNG
|
||||
AUKN_SYM AuUInt8 RngByte();
|
||||
AUKN_SYM bool RngBoolean();
|
||||
AUKN_SYM AuUInt32 RngU32();
|
||||
AUKN_SYM AuUInt32 RngU32(AuUInt32 uMin, AuUInt32 uMax);
|
||||
AUKN_SYM AuUInt32 RngU32Range(AuUInt32 uMin, AuUInt32 uMax);
|
||||
AUKN_SYM AuUInt64 RngU64();
|
||||
AUKN_SYM AuInt32 RngInt(AuInt32 uMin, AuInt32 uMax);
|
||||
AUKN_SYM AuInt32 RngI32Range(AuInt32 iMin, AuInt32 iMax);
|
||||
AUKN_SYM double RngDecimal();
|
||||
AUKN_SYM float RngNumber(float min, float max);
|
||||
AUKN_SYM AuUInt32 RngIndex(AuUInt32 count /* = max + 1*/);
|
||||
AUKN_SYM double RngNumber(double dMin, double dMax);
|
||||
AUKN_SYM AuUInt32 RngIndex(AuUInt32 uCount /* = max + 1*/);
|
||||
AUKN_SYM uuids::uuid RngUUID();
|
||||
|
||||
AUKN_SYM AuList<AuInt32> RngArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax);
|
||||
AUKN_SYM AuList<AuUInt32> RngArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax);
|
||||
AUKN_SYM AuList<double> RngArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax);
|
||||
AUKN_SYM AuList<AuInt32> RngArrayI32(AuUInt32 uCount);
|
||||
AUKN_SYM AuList<AuUInt32> RngArrayU32(AuUInt32 uCount);
|
||||
AUKN_SYM AuList<double> RngArrayDouble(AuUInt32 uCount);
|
||||
AUKN_SYM AuList<double> RngArrayDecimals(AuUInt32 uCount);
|
||||
AUKN_SYM AuList<uuids::uuid> RngArrayUUIDs(AuUInt32 uCount);
|
||||
// Note: it is conceivable that someone may want the following templates for some cryptographic purpose
|
||||
|
||||
template<bool fast = true, typename T, int N>
|
||||
|
@ -38,9 +38,9 @@ namespace Aurora::RNG
|
||||
return gFastDevice->NextU32();
|
||||
}
|
||||
|
||||
AUKN_SYM AuUInt32 RngU32(AuUInt32 uMin, AuUInt32 uMax)
|
||||
AUKN_SYM AuUInt32 RngU32Range(AuUInt32 uMin, AuUInt32 uMax)
|
||||
{
|
||||
return gFastDevice->NextU32(uMin, uMax);
|
||||
return gFastDevice->NextU32Range(uMin, uMax);
|
||||
}
|
||||
|
||||
AUKN_SYM AuUInt64 RngU64()
|
||||
@ -48,9 +48,9 @@ namespace Aurora::RNG
|
||||
return gFastDevice->NextU64();
|
||||
}
|
||||
|
||||
AUKN_SYM AuInt32 RngInt(AuInt32 uMin, AuInt32 uMax)
|
||||
AUKN_SYM AuInt32 RngI32Range(AuInt32 uMin, AuInt32 uMax)
|
||||
{
|
||||
return gFastDevice->NextInt(uMin, uMax);
|
||||
return gFastDevice->NextI32Range(uMin, uMax);
|
||||
}
|
||||
|
||||
AUKN_SYM double RngDecimal()
|
||||
@ -58,7 +58,7 @@ namespace Aurora::RNG
|
||||
return gFastDevice->NextDecimal();
|
||||
}
|
||||
|
||||
AUKN_SYM float RngNumber(float min, float max)
|
||||
AUKN_SYM double RngNumber(double min, double max)
|
||||
{
|
||||
return gFastDevice->NextNumber(min, max);
|
||||
}
|
||||
@ -72,4 +72,44 @@ namespace Aurora::RNG
|
||||
{
|
||||
return gFastDevice->NextUUID();
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<AuInt32> RngArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax)
|
||||
{
|
||||
return gFastDevice->NextArrayI32Range(uCount, iMin, iMax);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<AuUInt32> RngArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax)
|
||||
{
|
||||
return gFastDevice->NextArrayU32Range(uCount, uMin, uMax);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<double> RngArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax)
|
||||
{
|
||||
return gFastDevice->NextArrayDoubleRange(uCount, dMin, dMax);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<AuInt32> RngArrayI32(AuUInt32 uCount)
|
||||
{
|
||||
return gFastDevice->NextArrayI32(uCount);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<AuUInt32> RngArrayU32(AuUInt32 uCount)
|
||||
{
|
||||
return gFastDevice->NextArrayU32(uCount);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<double> RngArrayDouble(AuUInt32 uCount)
|
||||
{
|
||||
return gFastDevice->NextArrayDouble(uCount);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<double> RngArrayDecimals(AuUInt32 uCount)
|
||||
{
|
||||
return gFastDevice->NextArrayDecimals(uCount);
|
||||
}
|
||||
|
||||
AUKN_SYM AuList<uuids::uuid> RngArrayUUIDs(AuUInt32 uCount)
|
||||
{
|
||||
return gFastDevice->NextArrayUUIDs(uCount);
|
||||
}
|
||||
}
|
@ -81,6 +81,12 @@ namespace Aurora::RNG
|
||||
return 1ull << (ret + 1);
|
||||
}
|
||||
|
||||
static double RngConvertToDecimal(AuUInt64 uLargeInt)
|
||||
{
|
||||
AuUInt64 qwValue = (uLargeInt & 0xFFFFFFFFFFFFFull) | 0x3FF0000000000000ull;
|
||||
return *(double *)(&qwValue) - 1.0;
|
||||
}
|
||||
|
||||
void RandomDevice::NextString(char *pString, AuUInt32 uLength, ERngStringCharacters type)
|
||||
{
|
||||
static AuPair<const char *, int> rngSequence[static_cast<int>(ERngStringCharacters::eEnumCount)] =
|
||||
@ -178,7 +184,7 @@ namespace Aurora::RNG
|
||||
}
|
||||
}
|
||||
|
||||
AuInt32 RandomDevice::NextInt(AuInt32 uMin, AuInt32 uMax)
|
||||
AuInt32 RandomDevice::NextI32Range(AuInt32 uMin, AuInt32 uMax)
|
||||
{
|
||||
auto uRange = uMax - uMin;
|
||||
auto uMassiveWord = NextU32();
|
||||
@ -191,7 +197,7 @@ namespace Aurora::RNG
|
||||
return uMin + uNext;
|
||||
}
|
||||
|
||||
AuUInt32 RandomDevice::NextU32(AuUInt32 uMin, AuUInt32 uMax)
|
||||
AuUInt32 RandomDevice::NextU32Range(AuUInt32 uMin, AuUInt32 uMax)
|
||||
{
|
||||
auto uRange = uMax - uMin;
|
||||
auto uMassiveWord = NextU32();
|
||||
@ -234,7 +240,7 @@ namespace Aurora::RNG
|
||||
{
|
||||
#if defined(AURNG_USE_GARBAGE_DECIMALS)
|
||||
return (double(this->NextU32()) * (double(1.0) / double(AuNumericLimits<AuUInt32>::max()))) * udMax;
|
||||
#else
|
||||
#elif defined(AURNG_USE_UNIFORM_DECIMALS)
|
||||
union
|
||||
{
|
||||
double f;
|
||||
@ -279,12 +285,165 @@ namespace Aurora::RNG
|
||||
return x;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#if !defined(AURNG_USE_FAST_DECIMALS)
|
||||
#define AURNG_USE_FAST_DECIMALS
|
||||
return 0.0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
AuList<AuInt32> RandomDevice::NextArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax)
|
||||
{
|
||||
AuList<AuInt32> ret;
|
||||
AuList<AuUInt32> rngBytes;
|
||||
|
||||
rngBytes.resize(uCount);
|
||||
ret.resize(uCount);
|
||||
this->Read(rngBytes);
|
||||
|
||||
auto uRange = iMax - iMin;
|
||||
auto uUpperBound = RngConvertBounds(uRange + 1);
|
||||
for (AU_ITERATE_N(uIndex, rngBytes.size()))
|
||||
{
|
||||
auto uMassiveWord = rngBytes[uIndex];
|
||||
AuUInt32 uNext {};
|
||||
while ((uNext = (uMassiveWord & (uUpperBound - 1))) > uRange)
|
||||
{
|
||||
uMassiveWord = AuFnv1a32Runtime(&uMassiveWord, sizeof(uMassiveWord));
|
||||
}
|
||||
|
||||
ret[uIndex] = iMin + uNext;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
AuList<AuUInt32> RandomDevice::NextArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax)
|
||||
{
|
||||
AuList<AuUInt32> ret;
|
||||
AuList<AuUInt32> rngBytes;
|
||||
|
||||
ret.resize(uCount);
|
||||
rngBytes.resize(uCount);
|
||||
this->Read(rngBytes);
|
||||
|
||||
auto uRange = uMax - uMin;
|
||||
auto uUpperBound = RngConvertBounds(uRange + 1);
|
||||
|
||||
for (AU_ITERATE_N(uIndex, rngBytes.size()))
|
||||
{
|
||||
auto uMassiveWord = rngBytes[uIndex];
|
||||
AuUInt32 uNext {};
|
||||
while ((uNext = (uMassiveWord & (uUpperBound - 1))) > uRange)
|
||||
{
|
||||
uMassiveWord = AuFnv1a32Runtime(&uMassiveWord, sizeof(uMassiveWord));
|
||||
}
|
||||
|
||||
ret[uIndex] = uMin + uNext;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
AuList<double> RandomDevice::NextArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax)
|
||||
{
|
||||
#if defined(AURNG_USE_FAST_DECIMALS)
|
||||
AuList<double> ret;
|
||||
AuList<AuUInt64> rngBytes;
|
||||
|
||||
ret.resize(uCount);
|
||||
rngBytes.resize(uCount);
|
||||
this->Read(rngBytes);
|
||||
|
||||
double dRange = dMax - dMin;
|
||||
|
||||
for (AU_ITERATE_N(uIndex, rngBytes.size()))
|
||||
{
|
||||
double dValue = RngConvertToDecimal(rngBytes[uIndex]);
|
||||
dValue *= dRange;
|
||||
dValue += dMin;
|
||||
ret[uIndex] = dValue;
|
||||
}
|
||||
|
||||
return ret;
|
||||
#else
|
||||
AuList<double> ret;
|
||||
ret.resize(uCount);
|
||||
|
||||
for (AU_ITERATE_N(uIndex, uCount))
|
||||
{
|
||||
ret[uIndex] = this->NextNumber(dMin, dMax);
|
||||
}
|
||||
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
AuList<AuInt32> RandomDevice::NextArrayI32(AuUInt32 uCount)
|
||||
{
|
||||
AuList<AuInt32> ret;
|
||||
ret.resize(uCount);
|
||||
this->Read(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
AuList<AuUInt32> RandomDevice::NextArrayU32(AuUInt32 uCount)
|
||||
{
|
||||
AuList<AuUInt32> ret;
|
||||
ret.resize(uCount);
|
||||
this->Read(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
AuList<double> RandomDevice::NextArrayDouble(AuUInt32 uCount)
|
||||
{
|
||||
AuList<double> ret;
|
||||
ret.resize(uCount);
|
||||
this->Read(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
AuList<double> RandomDevice::NextArrayDecimals(AuUInt32 uCount)
|
||||
{
|
||||
#if defined(AURNG_USE_FAST_DECIMALS)
|
||||
AuList<double> ret;
|
||||
AuList<AuUInt64> rngBytes;
|
||||
|
||||
ret.resize(uCount);
|
||||
rngBytes.resize(uCount);
|
||||
this->Read(rngBytes);
|
||||
|
||||
for (AU_ITERATE_N(uIndex, rngBytes.size()))
|
||||
{
|
||||
ret[uIndex] = RngConvertToDecimal(rngBytes[uIndex]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
#else
|
||||
AuList<double> ret;
|
||||
ret.resize(uCount);
|
||||
|
||||
for (AU_ITERATE_N(uIndex, uCount))
|
||||
{
|
||||
ret[uIndex] = this->NextDecimal();
|
||||
}
|
||||
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
double RandomDevice::NextDecimal()
|
||||
{
|
||||
return this->UniformFloatInRange(kDblEpsilon, 1.0);
|
||||
#if defined(AURNG_USE_FAST_DECIMALS)
|
||||
return RngConvertToDecimal(this->NextU64());
|
||||
#elif defined(AURNG_USE_UNIFORM_DECIMALS)
|
||||
return this->UniformFloatInRange(kDblEpsilon, 1.0 + kDblEpsilon) - kDblEpsilon;
|
||||
#elif defined(AURNG_USE_GARBAGE_DECIMALS)
|
||||
return this->UniformFloatInRange(0, 1.0);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
AuUInt32 RandomDevice::NextIndex(AuUInt32 uCount /* = max + 1*/)
|
||||
@ -302,7 +461,37 @@ namespace Aurora::RNG
|
||||
double RandomDevice::NextNumber(double dMin, double dMax)
|
||||
{
|
||||
auto dRange = dMax - dMin;
|
||||
#if defined(AURNG_USE_FAST_DECIMALS)
|
||||
return (this->NextDecimal() * dRange) + dMin;
|
||||
#else
|
||||
// potentially not deterministic, thanks to runtime/platform deviations
|
||||
return this->UniformFloatInRange(kDblEpsilon, dRange + kDblEpsilon) + dMin - kDblEpsilon;
|
||||
#endif
|
||||
}
|
||||
|
||||
AuList<uuids::uuid> RandomDevice::NextArrayUUIDs(AuUInt32 uCount)
|
||||
{
|
||||
AuList<uuids::uuid> ret;
|
||||
AuList<AuUInt8> rngBytes;
|
||||
|
||||
ret.resize(uCount);
|
||||
rngBytes.resize(uCount * 16);
|
||||
this->Read(rngBytes);
|
||||
|
||||
for (AU_ITERATE_N(uIndex, uCount))
|
||||
{
|
||||
auto pBytes = rngBytes.data() + (uIndex * 16);
|
||||
|
||||
pBytes[8] &= 0xBF;
|
||||
pBytes[8] |= 0x80;
|
||||
|
||||
pBytes[6] &= 0x4F;
|
||||
pBytes[6] |= 0x40;
|
||||
|
||||
ret[uIndex] = uuids::uuid { pBytes, pBytes + 16 };
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uuids::uuid RandomDevice::NextUUID()
|
||||
|
@ -24,8 +24,17 @@ namespace Aurora::RNG
|
||||
bool NextBoolean() override;
|
||||
AuUInt32 NextU32() override;
|
||||
AuUInt64 NextU64() override;
|
||||
AuInt32 NextInt(AuInt32 iMin, AuInt32 iMax) override;
|
||||
AuUInt32 NextU32(AuUInt32 uMin, AuUInt32 uMax) override;
|
||||
AuInt32 NextI32Range(AuInt32 iMin, AuInt32 iMax) override;
|
||||
AuUInt32 NextU32Range(AuUInt32 uMin, AuUInt32 uMax) override;
|
||||
AuList<AuInt32> NextArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax) override;
|
||||
AuList<AuUInt32> NextArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax) override;
|
||||
AuList<double> NextArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax) override;
|
||||
AuList<AuInt32> NextArrayI32(AuUInt32 uCount) override;
|
||||
AuList<AuUInt32> NextArrayU32(AuUInt32 uCount) override;
|
||||
AuList<double> NextArrayDouble(AuUInt32 uCount) override;
|
||||
AuList<double> NextArrayDecimals(AuUInt32 uCount) override;
|
||||
AuList<uuids::uuid> NextArrayUUIDs(AuUInt32 uCount) override;
|
||||
|
||||
double NextDecimal() override;
|
||||
double NextNumber(double dMin, double dMax) override;
|
||||
AuUInt32 NextIndex(AuUInt32 uCount /* = max + 1*/) override;
|
||||
|
Loading…
Reference in New Issue
Block a user