/usr/include/shogun/mathematics/Random.h is in libshogun-dev 3.2.0-7.3build4.
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 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Viktor Gal
* Copyright (C) 2013 Viktor Gal
*/
#ifndef __RANDOM_H__
#define __RANDOM_H__
#include <shogun/base/SGObject.h>
#include <shogun/lib/config.h>
#include <shogun/lib/Lock.h>
#include <limits>
/* opaque pointers */
struct SFMT_T;
struct DSFMT_T;
namespace shogun
{
class CLock;
/** @brief: Pseudo random number geneartor
*
* It is based on SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
* number generator.
*
* */
class CRandom : public CSGObject
{
public:
/** default ctor */
CRandom();
/** ctor
* @param seed the seed for the PRNG
*/
CRandom(uint32_t seed);
/** dtor */
virtual ~CRandom();
/** set seed
*
* @param seed seed for PRNG
*/
void set_seed(uint32_t seed);
/** get seed
*
* @return seed
*/
uint32_t get_seed() const;
/**
* Generate an unsigned 32-bit random integer
*
* @return the random 32-bit unsigned integer
*/
uint32_t random_32() const;
/**
* Generate an unsigned 64-bit random integer
*
* @return the random 64-bit unsigned integer
*/
uint64_t random_64() const;
/**
* Generate a signed 32-bit random integer
*
* @return the random 32-bit signed integer
*/
inline int32_t random_s32() const
{
return random_32() & ((uint32_t(-1)<<1)>>1);
}
/**
* Generate a signed 64-bit random integer
*
* @return the random 64-bit signed integer
*/
int64_t random_s64() const
{
return random_64() & ((uint64_t(-1)<<1)>>1);
}
/** generate an unsigned 64bit integer in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline uint64_t random(uint64_t min_value, uint64_t max_value)
{
return min_value + random_64() % (max_value-min_value+1);
}
/** generate an signed 64bit integer in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline int64_t random(int64_t min_value, int64_t max_value)
{
return min_value + random_s64() % (max_value-min_value+1);
}
/** generate an unsigned signed 32bit integer in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline uint32_t random(uint32_t min_value, uint32_t max_value)
{
return min_value + random_32() % (max_value-min_value+1);
}
/** generate an signed 32bit integer in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline int32_t random(int32_t min_value, int32_t max_value)
{
return min_value + random_s32() % (max_value-min_value+1);
}
/** generate an 32bit floating point number in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline float32_t random(float32_t min_value, float32_t max_value)
{
return min_value + ((max_value-min_value) * random_close());
}
/** generate an 64bit floating point number in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline float64_t random(float64_t min_value, float64_t max_value)
{
return min_value + ((max_value-min_value) * random_close());
}
/** generate an 96-128bit floating point number (depending on the
* size of floatmax_t) in the range
* [min_value, max_value] (closed interval!)
*
* @param min_value minimum value
* @param max_value maximum value
* @return random number
*/
inline floatmax_t random(floatmax_t min_value, floatmax_t max_value)
{
return min_value + ((max_value-min_value) * random_close());
}
/**
* Fill an array of unsinged 32 bit integer
*
* @param array 32-bit unsigened int array to be filled
* @param size size of the array
*/
void fill_array(uint32_t* array, int32_t size) const;
/**
* Fill an array of unsinged 64 bit integer
*
* @param array 64-bit unsigened int array to be filled
* @param size size of the array
*/
void fill_array(uint64_t* array, int32_t size) const;
/**
* Fills an array of float64_t with randoms
* from the (0,1] interval
*
* @param array
* @param size
*/
void fill_array_oc(float64_t* array, int32_t size) const;
/**
* Fills an array of float64_t with randoms
* from the [0,1) interval
*
* @param array
* @param size
*/
void fill_array_co(float64_t* array, int32_t size) const;
/**
* Fills an array of float64_t with randoms
* from the (0,1) interval
*
* @param array
* @param size
*/
void fill_array_oo(float64_t* array, int32_t size) const;
/**
* Fills an array of float64_t with randoms
* from the [1,2) interval
*
* @param array
* @param size
*/
void fill_array_c1o2(float64_t* array, int32_t size) const;
/**
* Get random
* @return a float64_t random from [0,1] interval
*/
float64_t random_close() const;
/**
* Get random
* @return a float64_t random from (0,1) interval
*/
float64_t random_open() const;
/**
* Get random
*
* @return a float64_t random from [0,1) interval
*/
float64_t random_half_open() const;
/**
* Sample a normal distrbution.
* Using Ziggurat algorithm
*
* @param mu mean
* @param sigma variance
* @return sample from the desired normal distrib
*/
float64_t normal_distrib(float64_t mu, float64_t sigma) const;
/**
* Sample a standard normal distribution,
* i.e. mean = 0, var = 1.0
*
* @return sample from the std normal distrib
*/
float64_t std_normal_distrib() const;
/**
* Generate a seed for PRNG
*
* @return entropy for PRNG
*/
static uint32_t generate_seed();
virtual const char* get_name() const { return "Random"; }
private:
/** initialise the object */
void init();
/** reinit PRNG
*
* @param seed seed for the PRNG
*/
void reinit(uint32_t seed);
/**
* Sample from the distribution tail (defined as having x >= R).
*
* @return
*/
float64_t sample_tail() const;
/**
* Gaussian probability density function, denormailised, that is, y = e^-(x^2/2).
*/
float64_t GaussianPdfDenorm(float64_t x) const;
/**
* Inverse function of GaussianPdfDenorm(x)
*/
float64_t GaussianPdfDenormInv(float64_t y) const;
private:
/** seed */
uint32_t m_seed;
/** SFMT struct for 32-bit random */
SFMT_T* m_sfmt_32;
/** SFMT struct for 64-bit random */
SFMT_T* m_sfmt_64;
/** dSFMT struct */
DSFMT_T* m_dsfmt;
/** Number of blocks */
int32_t m_blockCount; //= 128;
/** Right hand x coord of the base rectangle, thus also the left hand x coord of the tail */
float64_t m_R;//= 3.442619855899;
/** Area of each rectangle (pre-determined/computed for 128 blocks). */
float64_t m_A;// = 9.91256303526217e-3;
/** Scale factor for converting a UInt with range [0,0xffffffff] to a double with range [0,1]. */
float64_t m_uint32ToU;// = 1.0 / (float64_t)UINT32_MAX;
/** Area A divided by the height of B0 */
float64_t m_A_div_y0;
/** top-right position ox rectangle i */
float64_t* m_x;
float64_t* m_y;
/** The proprtion of each segment that is entirely within the distribution, expressed as uint where
a value of 0 indicates 0% and uint.MaxValue 100%. Expressing this as an integer allows some floating
points operations to be replaced with integer ones.
*/
uint32_t* m_xComp;
/** state lock */
CLock m_state_lock;
};
}
#endif /* __RANDOM_H__ */
|