ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/rng.h
(Generate patch)

Comparing deliantra/server/include/rng.h (file contents):
Revision 1.5 by root, Mon Oct 25 11:35:14 2010 UTC vs.
Revision 1.16 by root, Wed Dec 5 21:18:37 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines