chsvlib
chsv helper source code

◆ RandomizeMemory_BE()

unsigned long long Chusov::Memory::RandomizeMemory_BE ( unsigned long long  nSeed,
void *  pMemory,
std::size_t  cbMemory 
)
noexcept

Randomize a memory buffer writing the least significant byte of a random value to the end of the buffer.

Parameters
nSeedA seed for the internal linear congruential generator. The value can be retrieved by a call to the GetDefaultSeed function. The value is specified in the Little-Endian byte order.
[out]pMemoryA pointer to memory to randomize.
cbMemorySize of the buffer pointed to by pMemory, in bytes.
Returns
The function returns the last random bytes written to the buffer, in the Little-Endian byte order. If cbMemory is less than sizeof(unsigned long long), the return value will include highest-order bytes of nSeed as lowest-order bytes of the return value, together with lowest-order random bytes as the highest-order bytes of the return value. Generally, if the destination buffer is not a multiple of words, that is cbMemory is not a multiple of sizeof(unsigned long long), the return value will be composed of the two lastly-written words, assuming the existence of a pseudo-element with the value of nSeed at the region of memory just before pMemory. On the other hand, if cbMemory is a multiple of sizeof(unsigned long long), then the return value can be used as a seed for subsedquent calls to RandomizeMemory_BE in a chain used to randomize several buffers using consistent pseudo-random generator state formed from an initial seed.

The pseudo-random number generator used by the function is a linear congruential generator \(\forall n \ge 0, x_n = (A \cdot x_{n-1} + B) \mod 2^{64}\), where \(x_{-1}\) equals nSeed, \(A\) is 0xD1342543DE82EF95 and \(B\) is 5. The function writes cbMemory lowest-order bytes of the value \(X = \sum_{n=0}^{\infty} x_n \cdot 2^{64n}\) to pMemory in the Big-Endian byte order.

See also
RandomizeMemory_LE.