/usr/include/stxxl/bits/common/rand.h is in libstxxl-dev 1.4.1-2.
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 | /***************************************************************************
* include/stxxl/bits/common/rand.h
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2002, 2003, 2005 Roman Dementiev <dementiev@mpi-sb.mpg.de>
* Copyright (C) 2007 Andreas Beckmann <beckmann@mpi-inf.mpg.de>
* Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
#ifndef STXXL_COMMON_RAND_HEADER
#define STXXL_COMMON_RAND_HEADER
#include <cmath>
#include <stxxl/bits/config.h>
#include <stxxl/bits/common/types.h>
#include <stxxl/bits/common/seed.h>
#include <stxxl/bits/namespace.h>
#if STXXL_STD_RANDOM
#include <random>
#elif STXXL_BOOST_RANDOM
#include <boost/random.hpp>
#endif
// Recommended seeding procedure:
// by default, the global seed is initialized from a high resolution timer and the process id
// 1. stxxl::set_seed(seed); // optionally, do this if you wan't to us a specific seed to replay a certain program run
// 2. seed = stxxl::get_next_seed(); // store/print/... this value can be used for step 1 to replay the program with a specific seed
// 3. stxxl::srandom_number32(); // seed the global state of stxxl::random_number32
// 4. create all the other prngs used.
STXXL_BEGIN_NAMESPACE
extern unsigned ran32State;
//! \addtogroup support
//! \{
//! Fast uniform [0, 2^32) pseudo-random generator with period 2^32, random
//! bits: 32.
//! \warning Uses a global state and is not reentrant or thread-safe!
struct random_number32
{
typedef unsigned value_type;
//! Returns a random number from [0, 2^32)
inline value_type operator () () const
{
return (ran32State = 1664525 * ran32State + 1013904223);
}
//! Returns a random number from [0, N)
inline value_type operator () (const value_type& N) const
{
return operator () () % N;
}
};
//! Set a seed value for \c random_number32.
inline void srandom_number32(unsigned seed = 0)
{
if (!seed)
seed = get_next_seed();
ran32State = seed;
}
//! Fast uniform [0, 2^32) pseudo-random generator with period 2^32, random
//! bits: 32.
//! Reentrant variant of random_number32 that keeps it's private state.
struct random_number32_r
{
typedef unsigned value_type;
mutable unsigned state;
random_number32_r(unsigned seed = 0)
{
if (!seed)
seed = get_next_seed();
state = seed;
}
//! Returns a random number from [0, 2^32)
inline value_type operator () () const
{
return (state = 1664525 * state + 1013904223);
}
};
//! Fast uniform [0, 255] pseudo-random generator with period 2^8, random bits:
//! 8 (one byte).
class random_number8_r
{
random_number32_r m_rnd32;
uint32 m_value;
unsigned int m_pos;
public:
typedef uint8 value_type;
random_number8_r(unsigned seed = 0)
: m_rnd32(seed), m_pos(4)
{ }
//! Returns a random byte from [0, 255]
inline value_type operator () ()
{
if (++m_pos >= 4) {
m_value = m_rnd32();
m_pos = 0;
}
return ((uint8*)&m_value)[m_pos];
}
};
//! Fast uniform [0.0, 1.0) pseudo-random generator
//! \warning Uses a global state and is not reentrant or thread-safe!
struct random_uniform_fast
{
typedef double value_type;
random_number32 rnd32;
random_uniform_fast(unsigned /*seed*/ = 0)
{ }
//! Returns a random number from [0.0, 1.0)
inline value_type operator () () const
{
return (double(rnd32()) * (0.5 / 0x80000000));
}
};
#if STXXL_MSVC
#pragma warning(push)
#pragma warning(disable:4512) // assignment operator could not be generated
#endif
//! Slow and precise uniform [0.0, 1.0) pseudo-random generator
//! period: at least 2^48, random bits: at least 31
//!
//! \warning Seed is not the same as in the fast generator \c random_uniform_fast
struct random_uniform_slow
{
typedef double value_type;
#if STXXL_STD_RANDOM
typedef std::default_random_engine gen_type;
mutable gen_type gen;
typedef std::uniform_real_distribution<> uni_type;
mutable uni_type uni;
random_uniform_slow(unsigned seed = 0)
: gen(seed ? seed : get_next_seed()),
uni(0.0, 1.0)
{ }
#elif STXXL_BOOST_RANDOM
typedef boost::minstd_rand base_generator_type;
base_generator_type generator;
boost::uniform_real<> uni_dist;
mutable boost::variate_generator<base_generator_type&, boost::uniform_real<> > uni;
random_uniform_slow(unsigned seed = 0) : uni(generator, uni_dist)
{
if (!seed)
seed = get_next_seed();
uni.engine().seed(seed);
}
#else
mutable unsigned short state48[3];
/*
* embedded erand48.c
*
* Copyright (c) 1993 Martin Birgmeier
* All rights reserved.
*
* You may redistribute unmodified or modified versions of this source
* code provided that the above copyright notice and this and the
* following conditions are retained.
*
* This software is provided ``as is'', and comes with no warranties
* of any kind. I shall in no event be liable for anything that happens
* to anyone/anything when using this software.
*/
static void
_dorand48(unsigned short xseed[3])
{
unsigned long accu;
unsigned short temp[2];
static const unsigned short _mult[3] = { 0xe66d, 0xdeec, 0x0005 };
static const unsigned short _add = 0x000b;
accu = (unsigned long)_mult[0] * (unsigned long)xseed[0]
+ (unsigned long)_add;
temp[0] = (unsigned short)accu; /* lower 16 bits */
accu >>= sizeof(unsigned short) * 8;
accu += (unsigned long)_mult[0] * (unsigned long)xseed[1]
+ (unsigned long)_mult[1] * (unsigned long)xseed[0];
temp[1] = (unsigned short)accu; /* middle 16 bits */
accu >>= sizeof(unsigned short) * 8;
accu += _mult[0] * xseed[2] + _mult[1] * xseed[1] + _mult[2] * xseed[0];
xseed[0] = temp[0];
xseed[1] = temp[1];
xseed[2] = (unsigned short)accu;
}
static double
_erand48(unsigned short xseed[3])
{
_dorand48(xseed);
return ldexp((double)xseed[0], -48)
+ ldexp((double)xseed[1], -32)
+ ldexp((double)xseed[2], -16);
}
/* end erand48.c */
random_uniform_slow(unsigned seed = 0)
{
if (!seed)
seed = get_next_seed();
state48[0] = (unsigned short)(seed & 0xffff);
state48[1] = (unsigned short)(seed >> 16);
state48[2] = 42;
_dorand48(state48);
}
#endif
//! Returns a random number from [0.0, 1.0)
inline value_type operator () () const
{
#if STXXL_STD_RANDOM
return uni(gen);
#elif STXXL_BOOST_RANDOM
return uni();
#else
return _erand48(state48);
#endif
}
};
//! Uniform [0, N) pseudo-random generator
template <class UniformRGen = random_uniform_fast>
struct random_number
{
typedef unsigned value_type;
UniformRGen uniform;
random_number(unsigned seed = 0) : uniform(seed)
{ }
//! Returns a random number from [0, N)
inline value_type operator () (value_type N) const
{
return static_cast<value_type>(uniform() * double(N));
}
};
//! Slow and precise uniform [0, 2^64) pseudo-random generator
struct random_number64
{
typedef stxxl::uint64 value_type;
random_uniform_slow uniform;
random_number64(unsigned seed = 0) : uniform(seed)
{ }
//! Returns a random number from [0, 2^64)
inline value_type operator () () const
{
return static_cast<value_type>(uniform() * (18446744073709551616.));
}
//! Returns a random number from [0, N)
inline value_type operator () (value_type N) const
{
return static_cast<value_type>(uniform() * double(N));
}
};
#if STXXL_MSVC
#pragma warning(pop) // assignment operator could not be generated
#endif
//! \}
STXXL_END_NAMESPACE
#endif // !STXXL_COMMON_RAND_HEADER
|