- Implement cases 2 & 4 of RtlSplayTree
[reactos.git] / reactos / lib / rtl / random.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * PURPOSE: Random number generator functions
5 * FILE: lib/rtl/random.c
6 * PROGRAMMER:
7 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include <rtl.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 static ULONG SavedValue[128] =
17 {
18 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, /* 0 */
19 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa, /* 4 */
20 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, /* 8 */
21 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09, /* 12 */
22 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, /* 16 */
23 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311, /* 20 */
24 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, /* 24 */
25 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82, /* 28 */
26 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, /* 32 */
27 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd, /* 36 */
28 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, /* 40 */
29 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52, /* 44 */
30 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, /* 48 */
31 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb, /* 52 */
32 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, /* 56 */
33 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e, /* 60 */
34 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, /* 64 */
35 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0, /* 68 */
36 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, /* 72 */
37 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd, /* 76 */
38 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, /* 80 */
39 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35, /* 84 */
40 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, /* 88 */
41 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379, /* 92 */
42 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, /* 96 */
43 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd, /* 100 */
44 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, /* 104 */
45 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b, /* 108 */
46 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, /* 112 */
47 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b, /* 116 */
48 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, /* 120 */
49 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d /* 124 */
50 };
51
52
53 /*************************************************************************
54 * RtlRandom [NTDLL.@]
55 *
56 * Generates a random number
57 *
58 * PARAMS
59 * Seed [O] The seed of the Random function
60 *
61 * RETURNS
62 * It returns a random number distributed over [0..MAXLONG-1].
63 */
64 /*
65 * @implemented
66 */
67 ULONG NTAPI
68 RtlRandom (IN OUT PULONG Seed)
69 {
70 ULONG Rand;
71 int Pos;
72 ULONG Result;
73
74 PAGED_CODE_RTL();
75
76 Rand = (*Seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
77 *Seed = (Rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
78 Pos = *Seed & 0x7f;
79 Result = SavedValue[Pos];
80 SavedValue[Pos] = Rand;
81
82 return Result;
83 }
84
85 /*
86 * @unimplemented
87 */
88 ULONG
89 NTAPI
90 RtlRandomEx(
91 PULONG Seed
92 )
93 {
94 UNIMPLEMENTED;
95 return 0;
96 }
97
98
99
100 /*************************************************************************
101 * RtlUniform [NTDLL.@]
102 *
103 * Generates an uniform random number
104 *
105 * PARAMS
106 * Seed [O] The seed of the Random function
107 *
108 * RETURNS
109 * It returns a random number uniformly distributed over [0..MAXLONG-1].
110 *
111 * NOTES
112 * Generates an uniform random number using D.H. Lehmer's 1948 algorithm.
113 * In our case the algorithm is:
114 *
115 * Result = (*Seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
116 *
117 * *Seed = Result;
118 *
119 * DIFFERENCES
120 * The native documentation states that the random number is
121 * uniformly distributed over [0..MAXLONG]. In reality the native
122 * function and our function return a random number uniformly
123 * distributed over [0..MAXLONG-1].
124 */
125 ULONG NTAPI
126 RtlUniform (PULONG Seed)
127 {
128 ULONG Result;
129
130 /*
131 * Instead of the algorithm stated above, we use the algorithm
132 * below, which is totally equivalent (see the tests), but does
133 * not use a division and therefore is faster.
134 */
135 Result = *Seed * 0xffffffed + 0x7fffffc3;
136
137 if (Result == 0xffffffff || Result == 0x7ffffffe)
138 {
139 Result = (Result + 2) & MAXLONG;
140 }
141 else if (Result == 0x7fffffff)
142 {
143 Result = 0;
144 }
145 else if ((Result & 0x80000000) == 0)
146 {
147 Result = Result + (~Result & 1);
148 }
149 else
150 {
151 Result = (Result + (Result & 1)) & MAXLONG;
152 }
153
154 *Seed = Result;
155
156 return Result;
157 }
158 /* EOF */