ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

random.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:         See COPYING in the top level directory
00003  * PROJECT:           ReactOS system libraries
00004  * PURPOSE:           Random number generator functions
00005  * FILE:              lib/rtl/random.c
00006  * PROGRAMMER:
00007  */
00008 
00009 /* INCLUDES *****************************************************************/
00010 
00011 #include <rtl.h>
00012 
00013 #define NDEBUG
00014 #include <debug.h>
00015 
00016 static ULONG SavedValue[128] =
00017    {
00018       0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626,  /*   0 */
00019       0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,  /*   4 */
00020       0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8,  /*   8 */
00021       0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,  /*  12 */
00022       0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5,  /*  16 */
00023       0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,  /*  20 */
00024       0x404331a9, 0x74d97781, 0x64495118, 0x323e04be,  /*  24 */
00025       0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,  /*  28 */
00026       0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4,  /*  32 */
00027       0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,  /*  36 */
00028       0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016,  /*  40 */
00029       0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,  /*  44 */
00030       0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c,  /*  48 */
00031       0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,  /*  52 */
00032       0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8,  /*  56 */
00033       0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,  /*  60 */
00034       0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb,  /*  64 */
00035       0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,  /*  68 */
00036       0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743,  /*  72 */
00037       0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,  /*  76 */
00038       0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78,  /*  80 */
00039       0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,  /*  84 */
00040       0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a,  /*  88 */
00041       0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,  /*  92 */
00042       0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d,  /*  96 */
00043       0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,  /* 100 */
00044       0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515,  /* 104 */
00045       0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,  /* 108 */
00046       0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975,  /* 112 */
00047       0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,  /* 116 */
00048       0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb,  /* 120 */
00049       0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d   /* 124 */
00050    };
00051 
00052 
00053 /*************************************************************************
00054  * RtlRandom   [NTDLL.@]
00055  *
00056  * Generates a random number
00057  *
00058  * PARAMS
00059  *  Seed [O] The seed of the Random function
00060  *
00061  * RETURNS
00062  *  It returns a random number distributed over [0..MAXLONG-1].
00063  */
00064 /*
00065 * @implemented
00066 */
00067 ULONG NTAPI
00068 RtlRandom (IN OUT PULONG Seed)
00069 {
00070    ULONG Rand;
00071    int Pos;
00072    ULONG Result;
00073 
00074    PAGED_CODE_RTL();
00075 
00076    Rand = (*Seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
00077    *Seed = (Rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
00078    Pos = *Seed & 0x7f;
00079    Result = SavedValue[Pos];
00080    SavedValue[Pos] = Rand;
00081 
00082    return Result;
00083 }
00084 
00085 /*
00086 * @implemented
00087 */
00088 ULONG
00089 NTAPI
00090 RtlRandomEx( IN OUT PULONG Seed
00091     )
00092 {
00093    ULONG Rand;
00094    int Pos;
00095    ULONG Result;
00096 
00097    PAGED_CODE_RTL();
00098 
00099    Rand = (*Seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
00100    *Seed = (Rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
00101    Pos = *Seed & 0x7f;
00102    Result = SavedValue[Pos];
00103    SavedValue[Pos] = Rand;
00104 
00105    return Result;
00106 }
00107 
00108 
00109 
00110 /*************************************************************************
00111  * RtlUniform   [NTDLL.@]
00112  *
00113  * Generates an uniform random number
00114  *
00115  * PARAMS
00116  *  Seed [O] The seed of the Random function
00117  *
00118  * RETURNS
00119  *  It returns a random number uniformly distributed over [0..MAXLONG-1].
00120  *
00121  * NOTES
00122  *  Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
00123  *  In our case the algorithm is:
00124  *
00125  *  Result = (*Seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
00126  *
00127  *  *Seed = Result;
00128  *
00129  * DIFFERENCES
00130  *  The native documentation states that the random number is
00131  *  uniformly distributed over [0..MAXLONG]. In reality the native
00132  *  function and our function return a random number uniformly
00133  *  distributed over [0..MAXLONG-1].
00134  */
00135 ULONG
00136 NTAPI
00137 RtlUniform(IN PULONG Seed)
00138 {
00139     ULONG Result;
00140 
00141     /* Generate the random number */
00142     Result = (*Seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
00143 
00144     /* Return it */
00145     *Seed = Result;
00146     return Result;
00147 }

Generated on Sun May 27 2012 04:36:22 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.