• R/O
  • SSH
  • HTTPS

xangband: Commit


Commit MetaInfo

Révision1835 (tree)
l'heure2013-03-04 23:10:31
Auteuriks

Message de Log

Change random number generation method to Xorshift (from hengband)

Change Summary

Modification

--- XAngband/trunk/src/z-rand.c (revision 1834)
+++ XAngband/trunk/src/z-rand.c (revision 1835)
@@ -64,7 +64,12 @@
6464 /*
6565 * Current "state" table for the "complex" RNG
6666 */
67-u32b Rand_state[RAND_DEG];
67+u32b Rand_state[RAND_DEG] = {
68+ 123456789,
69+ 362436069,
70+ 521288629,
71+ 88675123,
72+};
6873
6974 /*
7075 * Current "state" of the quick RNG - don't bother to put this in save files.
@@ -77,77 +82,16 @@
7782 */
7883 void Rand_state_init(u32b seed)
7984 {
80- int i, j;
85+ int i;
8186
82- /* Seed the table */
83- Rand_state[0] = seed;
84-
85- /* Propagate the seed */
86- for (i = 1; i < RAND_DEG; i++) Rand_state[i] = LCRNG(Rand_state[i-1]);
87-
88- /* Cycle the table ten times per degree */
89- for (i = 0; i < RAND_DEG * 10; i++)
90- {
91- /* Acquire the next index */
92- j = Rand_place + 1;
93- if (j == RAND_DEG) j = 0;
94-
95- /* Update the table, extract an entry */
96- Rand_state[j] += Rand_state[Rand_place];
97-
98- /* Advance the index */
99- Rand_place = j;
100- }
87+ /* Initialize Xorshift Algorithm RNG */
88+ for (i = 1; i <= 4; ++ i) {
89+ seed = 1812433253UL * (seed ^ (seed >> 30)) + i;
90+ Rand_state[i-1] = seed; }
10191 }
10292
10393
10494 /*
105- * Extract a "random" number from 0 to m-1, via "modulus"
106- *
107- * Note that "m" should probably be less than 500000, or the
108- * results may be rather biased towards low values.
109- */
110-s32b Rand_mod(s32b m)
111-{
112- int j;
113- u32b r;
114-
115- /* Hack -- simple case */
116- if (m <= 1) return (0);
117-
118- /* Use the "simple" RNG */
119- if (Rand_quick)
120- {
121- /* Cycle the generator */
122- r = (Rand_value = LCRNG(Rand_value));
123-
124- /* Mutate a 28-bit "random" number */
125- r = ((r >> 4) % m);
126- }
127-
128- /* Use the "complex" RNG */
129- else
130- {
131- /* Acquire the next index */
132- j = Rand_place + 1;
133- if (j == RAND_DEG) j = 0;
134-
135- /* Update the table, extract an entry */
136- r = (Rand_state[j] += Rand_state[Rand_place]);
137-
138- /* Advance the index */
139- Rand_place = j;
140-
141- /* Extract a "random" number */
142- r = ((r >> 4) % m);
143- }
144-
145- /* Use the value */
146- return (r);
147-}
148-
149-
150-/*
15195 * Extract a "random" number from 0 to m-1, via "division"
15296 *
15397 * This method selects "random" 28-bit numbers, and then uses
@@ -165,12 +109,12 @@
165109 /* Hack -- simple case */
166110 if (m <= 1) return (0);
167111
168- /* Partition size */
169- n = (0x10000000 / m);
170-
171112 /* Use a simple RNG */
172113 if (Rand_quick)
173114 {
115+ /* Partition size */
116+ n = (0x10000000 / m);
117+
174118 /* Wait for it */
175119 while (1)
176120 {
@@ -188,27 +132,16 @@
188132 /* Use a complex RNG */
189133 else
190134 {
191- /* Wait for it */
192- while (1)
193- {
194- int j;
135+ /* Xorshift Algorithm RNG */
136+ u32b t = Rand_state[0] ^ (Rand_state[0] << 11);
195137
196- /* Acquire the next index */
197- j = Rand_place + 1;
198- if (j == RAND_DEG) j = 0;
138+ Rand_state[0] = Rand_state[1];
139+ Rand_state[1] = Rand_state[2];
140+ Rand_state[2] = Rand_state[3];
199141
200- /* Update the table, extract an entry */
201- r = (Rand_state[j] += Rand_state[Rand_place]);
142+ Rand_state[3] = (Rand_state[3] ^ (Rand_state[3] >> 19)) ^ (t ^ (t >> 8));
202143
203- /* Hack -- extract a 28-bit "random" number */
204- r = (r >> 4) / n;
205-
206- /* Advance the index */
207- Rand_place = j;
208-
209- /* Done */
210- if (r < m) break;
211- }
144+ r = Rand_state[3] % m;
212145 }
213146
214147 /* Use the value */
--- XAngband/trunk/src/z-rand.h (revision 1834)
+++ XAngband/trunk/src/z-rand.h (revision 1835)
@@ -90,7 +90,6 @@
9090 /**** Available Functions ****/
9191
9292 extern void Rand_state_init(u32b seed);
93-extern s32b Rand_mod(s32b m);
9493 extern s32b Rand_div(u32b m);
9594 extern s16b randnor(int mean, int stand);
9695 extern s16b damroll(int num, int sides);
Afficher sur ancien navigateur de dépôt.