Jamie Reece Wilson
c16a0b1600
[+] 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)
122 lines
3.8 KiB
C++
122 lines
3.8 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: RNG.hpp
|
|
Date: 2021-7-14
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
#include "ERngStringCharacters.hpp"
|
|
#include "RandomDef.hpp"
|
|
#include "IRandomDevice.hpp"
|
|
|
|
namespace Aurora::RNG
|
|
{
|
|
AUKN_SYM void ReadSecureRNG(Memory::MemoryViewWrite writeView);
|
|
AUKN_SYM void ReadFastRNG (Memory::MemoryViewWrite writeView);
|
|
|
|
AUKN_SYM AuString ReadString(AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
|
|
AUKN_SYM void RngString(char *pString, AuUInt32 uLength, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
|
|
|
|
AUKN_SYM AuUInt8 RngByte();
|
|
AUKN_SYM bool RngBoolean();
|
|
AUKN_SYM AuUInt32 RngU32();
|
|
AUKN_SYM AuUInt32 RngU32Range(AuUInt32 uMin, AuUInt32 uMax);
|
|
AUKN_SYM AuUInt64 RngU64();
|
|
AUKN_SYM AuInt32 RngI32Range(AuInt32 iMin, AuInt32 iMax);
|
|
AUKN_SYM double RngDecimal();
|
|
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>
|
|
static auline void RngFillArray(T(&array)[N])
|
|
{
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(Memory::MemoryViewWrite(array));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(Memory::MemoryViewWrite(array));
|
|
}
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline void RngFillArray(T *array, AuUInt32 uCount)
|
|
{
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(Memory::MemoryViewWrite(array, uCount * sizeof(T)));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(Memory::MemoryViewWrite(array, uCount * sizeof(T)));
|
|
}
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline void RngFillRange(T &container)
|
|
{
|
|
RngFillArray<fast>(container.begin(), container.end() - container.begin());
|
|
}
|
|
|
|
static auline void RngFillBuffer(Memory::ByteBuffer &buffer)
|
|
{
|
|
auto view = buffer.GetNextLinearWrite();
|
|
RngFillRange(view);
|
|
buffer.writePtr += view.length;
|
|
}
|
|
|
|
/**
|
|
* @brief
|
|
* @deprecated
|
|
*/
|
|
static auline void RngFillRange(Memory::ByteBuffer &buffer)
|
|
{
|
|
RngFillBuffer(buffer);
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline T RngTmpl()
|
|
{
|
|
T ret {};
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(Memory::MemoryViewWrite(&ret, sizeof(T)));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(Memory::MemoryViewWrite(&ret, sizeof(T)));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngArray(T *pItems, AuUInt32 uCount)
|
|
{
|
|
return pItems[RngIndex(uCount)];
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngVector(AuList<T> &items)
|
|
{
|
|
return RngArray(items.data(), items.size());
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngRange(T &items)
|
|
{
|
|
return RngArray(items.begin(), items.end() - items.begin());
|
|
}
|
|
} |