1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #ifndef RNG_H__ |
23 | #ifndef RNG_H__ |
24 | #define RNG_H__ |
24 | #define RNG_H__ |
25 | |
25 | |
26 | #include <compiler.h> |
26 | #include <compiler.h> |
|
|
27 | |
|
|
28 | typedef uint32_t seed_t; // overkill |
27 | |
29 | |
28 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
30 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
29 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
31 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
30 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
32 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
31 | struct tausworthe_rng |
33 | struct tausworthe_rng |
… | |
… | |
75 | x = x * A + B; |
77 | x = x * A + B; |
76 | return x; |
78 | return x; |
77 | } |
79 | } |
78 | }; |
80 | }; |
79 | |
81 | |
80 | typedef lc_rng<3039177861U, 0> borosh_niederreiter_rng; |
82 | typedef lc_rng<3039177861U, 0U> borosh_niederreiter_rng; |
81 | typedef lc_rng<2147001325U, 715136305U> bcpl_rng; |
83 | typedef lc_rng<2147001325U, 715136305U> bcpl_rng; |
|
|
84 | typedef lc_rng< 1664525U, 1U> lavaux_janssens_rng; |
|
|
85 | typedef lc_rng< 1664525U, 1013904223U> numerical_recipes_rng; |
82 | |
86 | |
83 | template<typename T, int N, int k> |
87 | template<typename T, int N, int k> |
84 | struct gfsr_rng |
88 | struct gfsr_rng |
85 | { |
89 | { |
86 | int i; |
90 | int i; |
… | |
… | |
105 | } |
109 | } |
106 | while (i); |
110 | while (i); |
107 | } |
111 | } |
108 | |
112 | |
109 | // actually should subclass to gfsr32... :) |
113 | // actually should subclass to gfsr32... :) |
110 | void seed (uint32_t seed) |
114 | void seed (seed_t seed) |
111 | { |
115 | { |
112 | xorshift_rng rng; |
116 | xorshift_rng rng; |
113 | |
117 | |
114 | rng.seed (seed); |
118 | rng.seed (seed); |
115 | this->seed_rng (rng); |
119 | this->seed_rng (rng); |
… | |
… | |
134 | // and likely of higher quality. |
138 | // and likely of higher quality. |
135 | typedef gfsr_rng<uint32_t, 250, 103> r250_rng; |
139 | typedef gfsr_rng<uint32_t, 250, 103> r250_rng; |
136 | typedef gfsr_rng<uint32_t, 521, 168> r521_rng; |
140 | typedef gfsr_rng<uint32_t, 521, 168> r521_rng; |
137 | |
141 | |
138 | // freeciv uses this one, so it's good enough for us :) |
142 | // freeciv uses this one, so it's good enough for us :) |
|
|
143 | // (also known as mitchell moore generator) |
139 | typedef gfsr_rng<uint32_t, 55, 24> freeciv_rng; |
144 | typedef gfsr_rng<uint32_t, 55, 24> freeciv_rng; |
140 | |
145 | |
141 | // this one should be high quality, but is slightly slower than tausworthe |
146 | // this one should be high quality, but is slightly slower than tausworthe |
142 | struct r250521_rng |
147 | struct r250521_rng |
143 | { |
148 | { |
… | |
… | |
145 | r521_rng r521; |
150 | r521_rng r521; |
146 | |
151 | |
147 | void seed (uint32_t seed); |
152 | void seed (uint32_t seed); |
148 | uint32_t next (); |
153 | uint32_t next (); |
149 | }; |
154 | }; |
|
|
155 | |
|
|
156 | ///////////////////////////////////////////////////////////////////////////// |
150 | |
157 | |
151 | // this is actually an adaptor that provides different |
158 | // this is actually an adaptor that provides different |
152 | // distributions of random numbers. |
159 | // distributions of random numbers. |
153 | template<class generator> |
160 | template<class generator> |
154 | struct random_number_generator : generator |
161 | struct random_number_generator : generator |
155 | { |
162 | { |
156 | random_number_generator () |
163 | random_number_generator () |
157 | { |
164 | { |
158 | } |
165 | } |
159 | |
166 | |
160 | random_number_generator (uint32_t seed) |
167 | random_number_generator (seed_t seed) |
161 | { |
168 | { |
162 | this->seed (seed); |
169 | this->seed (seed); |
163 | } |
170 | } |
164 | |
171 | |
165 | // uniform distribution, [0 .. num - 1] |
172 | // uniform distribution, [0 .. num - 1] |