xbox-kernel/private/ntos/rtl/random.c
2020-09-30 17:17:25 +02:00

125 lines
3.4 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*++
Copyright (c) 1989 Microsoft Corporation
Module Name:
Random.c
Abstract:
This module implements a simple random number generator
Author:
Gary Kimura [GaryKi] 26-May-1989
Environment:
Pure utility routine
Revision History:
--*/
#include <ntrtlp.h>
#define Multiplier ((ULONG)(0x80000000ul - 19)) // 2**31 - 19
#define Increment ((ULONG)(0x80000000ul - 61)) // 2**31 - 61
#define Modulus ((ULONG)(0x80000000ul - 1)) // 2**31 - 1
ULONG RtlpRandomConstantVector[128] = {
0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d
};
#if !defined(NTOS_KERNEL_RUNTIME)
ULONG
RtlUniform (
IN OUT PULONG Seed
)
/*++
Routine Description:
A simple uniform random number generator, based on D.H. Lehmer's 1948
alrogithm.
Arguments:
Seed - Supplies a pointer to the random number generator seed.
Return Value:
ULONG - returns a random number uniformly distributed over [0..MAXLONG]
--*/
{
*Seed = ((Multiplier * (*Seed)) + Increment) % Modulus;
return *Seed;
}
#endif
#define UniformMacro(Seed) ( \
*Seed = (((Multiplier * (*Seed)) + Increment) % Modulus) \
)
ULONG
RtlRandom (
IN OUT PULONG Seed
)
/*++
Routine Description:
An every better random number generator based on MacLaren and Marsaglia.
Arguments:
Seed - Supplies a pointer to the random number generator seed.
Return Value:
ULONG - returns a random number uniformly distributed over [0..MAXLONG]
--*/
{
ULONG X;
ULONG Y;
ULONG j;
ULONG Result;
// RTL_PAGED_CODE();
X = UniformMacro(Seed);
Y = UniformMacro(Seed);
j = Y % 128;
Result = RtlpRandomConstantVector[j];
RtlpRandomConstantVector[j] = X;
return Result;
}