test: random.c Source File

Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# random.c

Go to the documentation of this file.

    00001 /*++
    00002
    00003 Copyright (c) 1989  Microsoft Corporation
    00004
    00005 Module Name:
    00006
    00007     Random.c
    00008
    00009 Abstract:
    00010
    00011     This module implements a simple random number generator
    00012
    00013 Author:
    00014
    00015     Gary Kimura     [GaryKi]    26-May-1989
    00016
    00017 Environment:
    00018
    00019     Pure utility routine
    00020
    00021 Revision History:
    00022
    00023 --*/
    00024
    00025 #include <ntrtlp.h>
    00026
    00027 #if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
    00028 #pragma alloc_text(PAGE, RtlRandom)
    00029 #endif
    00030
    00031 #define Multiplier ((ULONG)(0x80000000ul - 19)) // 2**31 - 19
    00032 #define Increment  ((ULONG)(0x80000000ul - 61)) // 2**31 - 61
    00033 #define Modulus    ((ULONG)(0x80000000ul - 1))  // 2**31 - 1
    00034
    00035 #if !defined(NTOS_KERNEL_RUNTIME)
    00036 ULONG
    00037 RtlUniform (
    00038     IN OUT PULONG Seed
    00039     )
    00040
    00041 /*++
    00042
    00043 Routine Description:
    00044
    00045     A simple uniform random number generator, based on D.H. Lehmer's 1948
    00046     alrogithm.
    00047
    00048 Arguments:
    00049
    00050     Seed - Supplies a pointer to the random number generator seed.
    00051
    00052 Return Value:
    00053
    00054     ULONG - returns a random number uniformly distributed over [0..MAXLONG]
    00055
    00056 --*/
    00057
    00058 {
    00059     *Seed = ((Multiplier * (*Seed)) + Increment) % Modulus;
    00060     return *Seed;
    00061 }
    00062 #endif
    00063
    00064 #define UniformMacro(Seed) (                                 \
    00065     *Seed = (((Multiplier * (*Seed)) + Increment) % Modulus) \
    00066     )
    00067
    00068
    00069 extern ULONG RtlpRandomConstantVector[];
    00070
    00071 ULONG
    00072 RtlRandom (
    00073     IN OUT PULONG Seed
    00074     )
    00075
    00076 /*++
    00077
    00078 Routine Description:
    00079
    00080     An every better random number generator based on MacLaren and Marsaglia.
    00081
    00082 Arguments:
    00083
    00084     Seed - Supplies a pointer to the random number generator seed.
    00085
    00086 Return Value:
    00087
    00088     ULONG - returns a random number uniformly distributed over [0..MAXLONG]
    00089
    00090 --*/
    00091
    00092 {
    00093     ULONG X;
    00094     ULONG Y;
    00095     ULONG j;
    00096     ULONG Result;
    00097
    00098     RTL_PAGED_CODE();
    00099
    00100     X = UniformMacro(Seed);
    00101     Y = UniformMacro(Seed);
    00102
    00103     j = Y % 128;
    00104
    00105     Result = RtlpRandomConstantVector[j];
    00106
    00107     RtlpRandomConstantVector[j] = X;
    00108
    00109     return Result;
    00110
    00111 }
    00112