SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Random.h
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2013 Viktor Gal
00008  * Copyright (C) 2013 Viktor Gal
00009  */
00010 
00011 #ifndef __RANDOM_H__
00012 #define __RANDOM_H__
00013 
00014 #include <shogun/base/SGObject.h>
00015 #include <shogun/lib/config.h>
00016 #include <shogun/lib/Lock.h>
00017 #include <limits>
00018 
00019 /* opaque pointers */
00020 struct SFMT_T;
00021 struct DSFMT_T;
00022 
00023 namespace shogun
00024 {
00025     class CLock;
00032     class CRandom : public CSGObject
00033     {
00034         public:
00036             CRandom();
00037 
00041             CRandom(uint32_t seed);
00042 
00044             virtual ~CRandom();
00045 
00050             void set_seed(uint32_t seed);
00051 
00056             uint32_t get_seed() const;
00057 
00063             uint32_t random_32() const;
00064 
00070             uint64_t random_64() const;
00071 
00077             inline int32_t random_s32() const
00078             {
00079                 return random_32() & ((uint32_t(-1)<<1)>>1);
00080             }
00081 
00087             int64_t random_s64() const
00088             {
00089                 return random_64() & ((uint64_t(-1)<<1)>>1);
00090             }
00091 
00092 
00100             inline uint64_t random(uint64_t min_value, uint64_t max_value)
00101             {
00102                 return min_value + random_64() % (max_value-min_value+1);
00103             }
00104 
00112             inline int64_t random(int64_t min_value, int64_t max_value)
00113             {
00114                 return min_value + random_s64() % (max_value-min_value+1);
00115             }
00116 
00124             inline uint32_t random(uint32_t min_value, uint32_t max_value)
00125             {
00126                 return min_value + random_32() % (max_value-min_value+1);
00127             }
00128 
00136             inline int32_t random(int32_t min_value, int32_t max_value)
00137             {
00138                 return min_value + random_s32() % (max_value-min_value+1);
00139             }
00140 
00148             inline float32_t random(float32_t min_value, float32_t max_value)
00149             {
00150                 return min_value + ((max_value-min_value) * random_close());
00151             }
00152 
00160             inline float64_t random(float64_t min_value, float64_t max_value)
00161             {
00162                 return min_value + ((max_value-min_value) * random_close());
00163             }
00164 
00173             inline floatmax_t random(floatmax_t min_value, floatmax_t max_value)
00174             {
00175                 return min_value + ((max_value-min_value) * random_close());
00176             }
00177 
00184             void fill_array(uint32_t* array, int32_t size) const;
00185 
00192             void fill_array(uint64_t* array, int32_t size) const;
00193 
00201             void fill_array_oc(float64_t* array, int32_t size) const;
00202 
00210             void fill_array_co(float64_t* array, int32_t size) const;
00211 
00219             void fill_array_oo(float64_t* array, int32_t size) const;
00220 
00229             void fill_array_c1o2(float64_t* array, int32_t size) const;
00230 
00235             float64_t random_close() const;
00236 
00241             float64_t random_open() const;
00242 
00248             float64_t random_half_open() const;
00249 
00258             float64_t normal_distrib(float64_t mu, float64_t sigma) const;
00259 
00266             float64_t std_normal_distrib() const;
00267 
00273             static uint32_t generate_seed();
00274 
00275             virtual const char* get_name() const { return "Random"; }
00276 
00277         private:
00279             void init();
00280 
00285              void reinit(uint32_t seed);
00286 
00292             float64_t sample_tail() const;
00293 
00297             float64_t GaussianPdfDenorm(float64_t x) const;
00298 
00302             float64_t GaussianPdfDenormInv(float64_t y) const;
00303 
00304         private:
00306             uint32_t m_seed;
00307 
00309             SFMT_T* m_sfmt_32;
00310 
00312             SFMT_T* m_sfmt_64;
00313 
00315             DSFMT_T* m_dsfmt;
00316 
00318             int32_t m_blockCount; //= 128;
00319 
00321         float64_t m_R;//= 3.442619855899;
00322 
00324             float64_t m_A;// = 9.91256303526217e-3;
00325 
00327             float64_t m_uint32ToU;// = 1.0 / (float64_t)UINT32_MAX;
00328 
00330             float64_t m_A_div_y0;
00331 
00333             float64_t* m_x;
00334             float64_t* m_y;
00335 
00340             uint32_t* m_xComp;
00341 
00343             CLock m_state_lock;
00344     };
00345 }
00346 
00347 #endif /* __RANDOM_H__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation