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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011 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. |
… | |
… | |
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; |
82 | |
85 | |
83 | template<typename T, int N, int k> |
86 | template<typename T, int N, int k> |
84 | struct gfsr_rng |
87 | struct gfsr_rng |
85 | { |
88 | { |
86 | int i; |
89 | int i; |
… | |
… | |
105 | } |
108 | } |
106 | while (i); |
109 | while (i); |
107 | } |
110 | } |
108 | |
111 | |
109 | // actually should subclass to gfsr32... :) |
112 | // actually should subclass to gfsr32... :) |
110 | void seed (uint32_t seed) |
113 | void seed (seed_t seed) |
111 | { |
114 | { |
112 | xorshift_rng rng; |
115 | xorshift_rng rng; |
113 | |
116 | |
114 | rng.seed (seed); |
117 | rng.seed (seed); |
115 | this->seed_rng (rng); |
118 | this->seed_rng (rng); |
… | |
… | |
134 | // and likely of higher quality. |
137 | // and likely of higher quality. |
135 | typedef gfsr_rng<uint32_t, 250, 103> r250_rng; |
138 | typedef gfsr_rng<uint32_t, 250, 103> r250_rng; |
136 | typedef gfsr_rng<uint32_t, 521, 168> r521_rng; |
139 | typedef gfsr_rng<uint32_t, 521, 168> r521_rng; |
137 | |
140 | |
138 | // freeciv uses this one, so it's good enough for us :) |
141 | // freeciv uses this one, so it's good enough for us :) |
|
|
142 | // (also known as mitchell moore generator |
139 | typedef gfsr_rng<uint32_t, 55, 24> freeciv_rng; |
143 | typedef gfsr_rng<uint32_t, 55, 24> freeciv_rng; |
140 | |
144 | |
141 | // this one should be high quality, but is slightly slower than tausworthe |
145 | // this one should be high quality, but is slightly slower than tausworthe |
142 | struct r250521_rng |
146 | struct r250521_rng |
143 | { |
147 | { |
… | |
… | |
146 | |
150 | |
147 | void seed (uint32_t seed); |
151 | void seed (uint32_t seed); |
148 | uint32_t next (); |
152 | uint32_t next (); |
149 | }; |
153 | }; |
150 | |
154 | |
|
|
155 | ///////////////////////////////////////////////////////////////////////////// |
|
|
156 | |
151 | // this is actually an adaptor that provides different types |
157 | // this is actually an adaptor that provides different |
152 | // of random numbers. |
158 | // distributions of random numbers. |
153 | template<class generator> |
159 | template<class generator> |
154 | struct random_number_generator : generator |
160 | struct random_number_generator : generator |
155 | { |
161 | { |
156 | random_number_generator () |
162 | random_number_generator () |
157 | { |
163 | { |
158 | } |
164 | } |
159 | |
165 | |
160 | random_number_generator (uint32_t seed) |
166 | random_number_generator (seed_t seed) |
161 | { |
167 | { |
162 | this->seed (seed); |
168 | this->seed (seed); |
163 | } |
169 | } |
164 | |
170 | |
165 | // uniform distribution, [0 .. num - 1] |
171 | // uniform distribution, [0 .. num - 1] |