/*** 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 { // May return any length between 1 and uBytes using RngGetBytesProvider_f = AuUInt32(*)(AuUInt8 *pBuffer, AuUInt32 uBytes); AUKN_SYM void SetSecureRNGBackendOverload(RngGetBytesProvider_f pSecureBackend); AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackendOverload(); AUKN_SYM RngGetBytesProvider_f GetSecureRNGBackend(); 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 AuInt32 RngI32Range(AuInt32 iMin, AuInt32 iMax); AUKN_SYM AuUInt64 RngU64Range(AuUInt64 uMin, AuUInt64 uMax); AUKN_SYM AuInt64 RngI64Range(AuInt64 iMin, AuInt64 iMax); AUKN_SYM AuVec2 RngVec2(AuVec2 boundA, AuVec2 boundB); AUKN_SYM AuVec2 RngVec2Sorted(AuVec2 min, AuVec2 max); AUKN_SYM AuVec3 RngVec3(AuVec3 boundA, AuVec3 boundB); AUKN_SYM AuVec3 RngVec3Sorted(AuVec3 min, AuVec3 max); AUKN_SYM AuVec4 RngVec4(AuVec4 boundA, AuVec4 boundB); AUKN_SYM AuVec4 RngVec4Sorted(AuVec4 min, AuVec4 max); AUKN_SYM AuUInt64 RngU64(); 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 RngArrayI32Range(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax); AUKN_SYM AuList RngArrayI32RangeFast(AuUInt32 uCount, AuInt32 iMin, AuInt32 iMax); AUKN_SYM AuList RngArrayU32Range(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax); AUKN_SYM AuList RngArrayU32RangeFast(AuUInt32 uCount, AuUInt32 uMin, AuUInt32 uMax); AUKN_SYM AuList RngArrayI64Range(AuUInt32 uCount, AuInt64 iMin, AuInt64 iMax); AUKN_SYM AuList RngArrayI64RangeFast(AuUInt32 uCount, AuInt64 iMin, AuInt64 iMax); AUKN_SYM AuList RngArrayU64Range(AuUInt32 uCount, AuUInt64 uMin, AuUInt64 uMax); AUKN_SYM AuList RngArrayU64RangeFast(AuUInt32 uCount, AuUInt64 uMin, AuUInt64 uMax); AUKN_SYM AuList RngArrayU32Mask(AuUInt32 uCount, AuUInt32 uMask); AUKN_SYM AuList RngArrayU64Mask(AuUInt32 uCount, AuUInt64 uMask); AUKN_SYM AuList RngArrayDoubleRange(AuUInt32 uCount, double dMin, double dMax); AUKN_SYM AuList RngArrayDoubleRangeFast(AuUInt32 uCount, double dMin, double dMax); AUKN_SYM AuList RngArrayI32(AuUInt32 uCount); AUKN_SYM AuList RngArrayU32(AuUInt32 uCount); AUKN_SYM AuList RngArrayI64(AuUInt32 uCount); AUKN_SYM AuList RngArrayU64(AuUInt32 uCount); AUKN_SYM AuList RngArrayVec2(AuUInt32 uCount, AuVec2 boundA, AuVec2 boundB); AUKN_SYM AuList RngArrayVec2Fast(AuUInt32 uCount, AuVec2 boundA, AuVec2 boundB); AUKN_SYM AuList RngArrayVec2Sorted(AuUInt32 uCount, AuVec2 min, AuVec2 max); AUKN_SYM AuList RngArrayVec2SortedFast(AuUInt32 uCount, AuVec2 min, AuVec2 max); AUKN_SYM AuList RngArrayVec3(AuUInt32 uCount, AuVec3 boundA, AuVec3 boundB); AUKN_SYM AuList RngArrayVec3Fast(AuUInt32 uCount, AuVec3 boundA, AuVec3 boundB); AUKN_SYM AuList RngArrayVec3Sorted(AuUInt32 uCount, AuVec3 min, AuVec3 max); AUKN_SYM AuList RngArrayVec3SortedFast(AuUInt32 uCount, AuVec3 min, AuVec3 max); AUKN_SYM AuList RngArrayVec4(AuUInt32 uCount, AuVec4 boundA, AuVec4 boundB); AUKN_SYM AuList RngArrayVec4Fast(AuUInt32 uCount, AuVec4 boundA, AuVec4 boundB); AUKN_SYM AuList RngArrayVec4Sorted(AuUInt32 uCount, AuVec4 min, AuVec4 max); AUKN_SYM AuList RngArrayVec4SortedFast(AuUInt32 uCount, AuVec4 min, AuVec4 max); AUKN_SYM AuList RngArrayDouble(AuUInt32 uCount); AUKN_SYM AuList RngArrayDecimals(AuUInt32 uCount); AUKN_SYM AuList RngArrayDecimalsFast(AuUInt32 uCount); AUKN_SYM AuList RngArrayUUIDs(AuUInt32 uCount); AUKN_SYM AuUInt RngArrayI32RangeInView(Memory::MemoryViewWrite write, AuInt32 iMin, AuInt32 iMax); AUKN_SYM AuUInt RngArrayI32RangeFastInView(Memory::MemoryViewWrite write, AuInt32 iMin, AuInt32 iMax); AUKN_SYM AuUInt RngArrayU32RangeInView(Memory::MemoryViewWrite write, AuUInt32 uMin, AuUInt32 uMax); AUKN_SYM AuUInt RngArrayU32RangeFastInView(Memory::MemoryViewWrite write, AuUInt32 uMin, AuUInt32 uMax); AUKN_SYM AuUInt RngArrayI64RangeInView(Memory::MemoryViewWrite write, AuInt64 iMin, AuInt64 iMax); AUKN_SYM AuUInt RngArrayI64RangeFastInView(Memory::MemoryViewWrite write, AuInt64 iMin, AuInt64 iMax); AUKN_SYM AuUInt RngArrayU64RangeInView(Memory::MemoryViewWrite write, AuUInt64 uMin, AuUInt64 uMax); AUKN_SYM AuUInt RngArrayU64RangeFastInView(Memory::MemoryViewWrite write, AuUInt64 uMin, AuUInt64 uMax); AUKN_SYM AuUInt RngArrayU32MaskInView(Memory::MemoryViewWrite write, AuUInt32 uMask); AUKN_SYM AuUInt RngArrayU64MaskInView(Memory::MemoryViewWrite write, AuUInt64 uMask); AUKN_SYM AuUInt RngArrayDoubleRangeInView(Memory::MemoryViewWrite write, double dMin, double dMax); AUKN_SYM AuUInt RngArrayDoubleRangeFastInView(Memory::MemoryViewWrite write, double dMin, double dMax); AUKN_SYM AuUInt RngArrayI32InView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayU32InView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayI64InView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayU64InView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayVec2InView(Memory::MemoryViewWrite write, AuVec2 boundA, AuVec2 boundB); AUKN_SYM AuUInt RngArrayVec2FastInView(Memory::MemoryViewWrite write, AuVec2 boundA, AuVec2 boundB); AUKN_SYM AuUInt RngArrayVec2SortedInView(Memory::MemoryViewWrite write, AuVec2 min, AuVec2 max); AUKN_SYM AuUInt RngArrayVec2SortedFastInView(Memory::MemoryViewWrite write, AuVec2 min, AuVec2 max); AUKN_SYM AuUInt RngArrayVec3InView(Memory::MemoryViewWrite write, AuVec3 boundA, AuVec3 boundB); AUKN_SYM AuUInt RngArrayVec3FastInView(Memory::MemoryViewWrite write, AuVec3 boundA, AuVec3 boundB); AUKN_SYM AuUInt RngArrayVec3SortedInView(Memory::MemoryViewWrite write, AuVec3 min, AuVec3 max); AUKN_SYM AuUInt RngArrayVec3SortedFastInView(Memory::MemoryViewWrite write, AuVec3 min, AuVec3 max); AUKN_SYM AuUInt RngArrayVec4InView(Memory::MemoryViewWrite write, AuVec4 boundA, AuVec4 boundB); AUKN_SYM AuUInt RngArrayVec4FastInView(Memory::MemoryViewWrite write, AuVec4 boundA, AuVec4 boundB); AUKN_SYM AuUInt RngArrayVec4SortedInView(Memory::MemoryViewWrite write, AuVec4 min, AuVec4 max); AUKN_SYM AuUInt RngArrayVec4SortedFastInView(Memory::MemoryViewWrite write, AuVec4 min, AuVec4 max); AUKN_SYM AuUInt RngArrayDoubleInView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayDecimalsInView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayDecimalsFastInView(Memory::MemoryViewWrite write); AUKN_SYM AuUInt RngArrayUUIDsInView(Memory::MemoryViewWrite write); // Note: it is conceivable that someone may want the following templates for some cryptographic purpose template static auline void RngFillArray(T(&array)[N]) { if constexpr (fast) { ReadFastRNG(Memory::MemoryViewWrite(array)); } else { ReadSecureRNG(Memory::MemoryViewWrite(array)); } } template 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 static auline void RngFillRange(T &container) { RngFillArray(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 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 static auline T &RngArray(T *pItems, AuUInt32 uCount) { return pItems[RngIndex(uCount)]; } template inline T RngIterator(T begin, T end) { auto nextItr = begin; auto uCount = std::distance(begin, end); std::advance(nextItr, RngIndex(uCount)); return nextItr; } template static auline auto RngRange(T &items) { return RngIterator(std::begin(items), std::end(items)); } template static auline T &RngList(AuList &items) { return *RngRange(items); } template static auline const T &RngList(const AuList &items) { return *RngRange(items); } template inline void RngShuffleIterators(T begin, T end) { AU_DEBUG_MEMCRUNCH; auto uCount = std::distance(begin, end); auto nextIndexArray = RngArrayU32RangeFast(uCount, 0, uCount - 1); for (AU_ITERATE_N(i, AuUInt(uCount))) { auto nextItrA = begin; auto nextItrB = begin; std::advance(nextItrA, i); std::advance(nextItrB, nextIndexArray[i]); AuSwap(*nextItrA, *nextItrB); } } template inline void RngShuffleList(AuList &list) { RngShuffleIterators(list.begin(), list.end()); } }