/usr/include/random/uniform.h is in libblitz0-dev 1:0.10-3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | // -*- C++ -*-
/***************************************************************************
* random/uniform.h Uniform IRNG wrapper class
*
* $Id$
*
* Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
*
* This file is a part of Blitz.
*
* Blitz is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Blitz is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Blitz. If not, see <http://www.gnu.org/licenses/>.
*
* Suggestions: blitz-devel@lists.sourceforge.net
* Bugs: blitz-support@lists.sourceforge.net
*
* For more information, please see the Blitz++ Home Page:
* https://sourceforge.net/projects/blitz/
*
***************************************************************************/
#ifndef BZ_RANDOM_UNIFORM_H
#define BZ_RANDOM_UNIFORM_H
#include <random/default.h>
#ifndef FLT_MANT_DIG
#include <float.h>
#endif
BZ_NAMESPACE(ranlib)
/*****************************************************************************
* UniformClosedOpen generator: uniform random numbers in [0,1).
*****************************************************************************/
template<typename T = defaultType, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class UniformClosedOpen { };
// These constants are 1/2^32, 1/2^64, 1/2^96, 1/2^128
const long double norm32open = .2328306436538696289062500000000000000000E-9L;
const long double norm64open = .5421010862427522170037264004349708557129E-19L;
const long double norm96open = .1262177448353618888658765704452457967477E-28L;
const long double norm128open = .2938735877055718769921841343055614194547E-38L;
template<typename IRNG, typename stateTag>
class UniformClosedOpen<float,IRNG,stateTag>
: public IRNGWrapper<IRNG,stateTag>
{
public:
typedef float T_numtype;
UniformClosedOpen() {};
UniformClosedOpen(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
float random()
{
#if FLT_MANT_DIG > 96
#if (FLT_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes float mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128open + i2 * norm96open + i3 * norm64open
+ i4 * norm32open;
#elif FLT_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96open + i2 * norm64open + i3 * norm32open;
#elif FLT_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64open + i2 * norm32open;
#else
IRNG_int i1 = this->irng_.random();
return i1 * norm32open;
#endif
}
float getUniform()
{ return random(); }
};
template<typename IRNG, typename stateTag>
class UniformClosedOpen<double,IRNG,stateTag>
: public IRNGWrapper<IRNG,stateTag>
{
public:
typedef double T_numtype;
UniformClosedOpen() {}
UniformClosedOpen(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
double random()
{
#if DBL_MANT_DIG > 96
#if (DBL_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes double mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128open + i2 * norm96open + i3 * norm64open
+ i4 * norm32open;
#elif DBL_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96open + i2 * norm64open + i3 * norm32open;
#elif DBL_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64open + i2 * norm32open;
#else
IRNG_int i1 = this->irng_.random();
return i1 * norm32open;
#endif
}
double getUniform() { return random(); }
};
template<typename IRNG, typename stateTag>
class UniformClosedOpen<long double,IRNG,stateTag>
: public IRNGWrapper<IRNG,stateTag>
{
public:
typedef long double T_numtype;
UniformClosedOpen() {};
UniformClosedOpen(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
long double random()
{
#if LDBL_MANT_DIG > 96
#if (LDBL_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes long double mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128open + i2 * norm96open + i3 * norm64open
+ i4 * norm32open;
#elif LDBL_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96open + i2 * norm64open + i3 * norm32open;
#elif LDBL_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64open + i2 * norm32open;
#else
IRNG_int i1 = this->irng_.random();
return i1 * norm32open;
#endif
}
long double getUniform() { return random(); }
};
// For people who don't care about open or closed: just give them
// ClosedOpen (this is the fastest).
template<class T, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class Uniform : public UniformClosedOpen<T,IRNG,stateTag>
{
public:
Uniform() {};
Uniform(unsigned int i) :
UniformClosedOpen<T,IRNG,stateTag>::UniformClosedOpen(i) {}
};
/*****************************************************************************
* UniformClosed generator: uniform random numbers in [0,1].
*****************************************************************************/
// This constant is 1/(2^32-1)
const long double norm32closed = .2328306437080797375431469961868475648078E-9L;
// These constants are 2^32/(2^64-1) and 1/(2^64-1), respectively.
const long double norm64closed1 =
.23283064365386962891887177448353618888727188481031E-9L;
const long double norm64closed2 =
.54210108624275221703311375920552804341370213034169E-19L;
// These constants are 2^64/(2^96-1), 2^32/(2^96-1), and 1/(2^96-1)
const long double norm96closed1 = .2328306436538696289062500000029387358771E-9L;
const long double norm96closed2 =
.5421010862427522170037264004418131333707E-19L;
const long double norm96closed3 =
.1262177448353618888658765704468388886588E-28L;
// These constants are 2^96/(2^128-1), 2^64/(2^128-1), 2^32/(2^128-1) and
// 1/(2^128-1).
const long double norm128clos1 = .2328306436538696289062500000000000000007E-9L;
const long double norm128clos2 = .5421010862427522170037264004349708557145E-19L;
const long double norm128clos3 = .1262177448353618888658765704452457967481E-28L;
const long double norm128clos4 = .2938735877055718769921841343055614194555E-38L;
template<typename T = double, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class UniformClosed { };
template<typename IRNG, typename stateTag>
class UniformClosed<float,IRNG,stateTag> : public IRNGWrapper<IRNG,stateTag> {
public:
typedef float T_numtype;
UniformClosed() {};
UniformClosed(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
float random()
{
#if FLTMANT_DIG > 96
#if (FLT_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes float mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128clos1 + i2 * norm128clos2
+ i3 * norm128clos3 + i4 * norm128clos4;
#elif FLT_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96closed1 + i2 * norm96closed2
+ i3 * norm96closed3;
#elif FLT_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64closed1 + i2 * norm64closed2;
#else
IRNG_int i = this->irng_.random();
return i * norm32closed;
#endif
}
float getUniform()
{ return random(); }
};
template<typename IRNG, typename stateTag>
class UniformClosed<double,IRNG,stateTag> : public IRNGWrapper<IRNG,stateTag> {
public:
typedef double T_numtype;
UniformClosed() {};
UniformClosed(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
double random()
{
#if DBL_MANT_DIG > 96
#if (DBL_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes double mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128clos1 + i2 * norm128clos2
+ i3 * norm128clos3 + i4 * norm128clos4;
#elif DBL_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96closed1 + i2 * norm96closed2
+ i3 * norm96closed3;
#elif DBL_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64closed1 + i2 * norm64closed2;
#else
IRNG_int i = this->irng_.random();
return i * norm32closed;
#endif
}
double getUniform()
{ return random(); }
};
template<typename IRNG, typename stateTag>
class UniformClosed<long double,IRNG,stateTag>
: public IRNGWrapper<IRNG,stateTag> {
public:
typedef long double T_numtype;
UniformClosed() {};
UniformClosed(unsigned int i) :
IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) {};
long double random()
{
#if LDBL_MANT_DIG > 96
#if (LDBL_MANT_DIG > 128) && !defined(BZ_IGNORE_RNG_ERRORS)
#error RNG code assumes long double mantissa is <= 128 bits (not true for your platform). Use -DBZ_IGNORE_RNG_ERRORS to ignore this warning.
#endif
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
IRNG_int i4 = this->irng_.random();
return i1 * norm128clos1 + i2 * norm128clos2
+ i3 * norm128clos3 + i4 * norm128clos4;
#elif LDBL_MANT_DIG > 64
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
IRNG_int i3 = this->irng_.random();
return i1 * norm96closed1 + i2 * norm96closed2
+ i3 * norm96closed3;
#elif LDBL_MANT_DIG > 32
IRNG_int i1 = this->irng_.random();
IRNG_int i2 = this->irng_.random();
return i1 * norm64closed1 + i2 * norm64closed2;
#else
IRNG_int i = this->irng_.random();
return i * norm32closed;
#endif
}
long double getUniform()
{ return random(); }
};
/*****************************************************************************
* UniformOpen generator: uniform random numbers in (0,1).
*****************************************************************************/
template<typename T = double, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class UniformOpen : public UniformClosedOpen<T,IRNG,stateTag> {
public:
typedef T T_numtype;
UniformOpen() {};
UniformOpen(unsigned int i) :
UniformClosedOpen<T,IRNG,stateTag>(i) {};
T random()
{
// Turn a [0,1) into a (0,1) interval by weeding out
// any zeros.
T x;
do {
x = UniformClosedOpen<T,IRNG,stateTag>::random();
} while (x == 0.0L);
return x;
}
T getUniform()
{ return random(); }
};
/*****************************************************************************
* UniformOpenClosed generator: uniform random numbers in (0,1]
*****************************************************************************/
template<typename T = double, typename IRNG = defaultIRNG,
typename stateTag = defaultState>
class UniformOpenClosed : public UniformClosedOpen<T,IRNG,stateTag> {
public:
typedef T T_numtype;
UniformOpenClosed() {};
UniformOpenClosed(unsigned int i) :
UniformClosedOpen<T,IRNG,stateTag>(i) {};
T random()
{
// Antithetic value: taking 1-X where X is [0,1) results
// in a (0,1] distribution.
return 1.0 - UniformClosedOpen<T,IRNG,stateTag>::random();
}
T getUniform()
{ return random(); }
};
BZ_NAMESPACE_END
#endif // BZ_RANDOM_UNIFORM_H
|