Random.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_RANDOM_H_
36 #define _BLAZE_UTIL_RANDOM_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <ctime>
44 #include <limits>
45 #include <boost/random/uniform_int.hpp>
46 #include <boost/random/uniform_real.hpp>
47 #include <boost/random/uniform_smallint.hpp>
48 #include <blaze/system/Random.h>
49 #include <blaze/util/Assert.h>
50 #include <blaze/util/Complex.h>
52 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // ::blaze NAMESPACE FORWARD DECLARATIONS
60 //
61 //=================================================================================================
62 
63 template< typename > class Rand;
64 
65 
66 
67 
68 //=================================================================================================
69 //
70 // CLASS DEFINITION
71 //
72 //=================================================================================================
73 
74 //*************************************************************************************************
138 template< typename Type > // Type of the random number generator
139 class Random : private NonCreatable
140 {
141  private:
142  //**Member variables****************************************************************************
145  static uint32_t seed_;
146  static Type rng_;
147 
148  //**********************************************************************************************
149 
150  //**Friend declarations*************************************************************************
152  template< typename T > friend class Rand;
153 // template< typename T > friend T rand();
154 // template< typename T > friend T rand( T min, T max );
155  friend uint32_t getSeed();
156  friend void setSeed( uint32_t seed );
158  //**********************************************************************************************
159 };
160 //*************************************************************************************************
161 
162 
163 
164 
165 //=================================================================================================
166 //
167 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
168 //
169 //=================================================================================================
170 
171 template< typename Type > uint32_t Random<Type>::seed_( static_cast<uint32_t>( std::time(0) ) );
172 template< typename Type > Type Random<Type>::rng_ ( seed_ );
173 
174 
175 
176 
177 //=================================================================================================
178 //
179 // CLASS RAND
180 //
181 //=================================================================================================
182 
183 //*************************************************************************************************
190 template< typename T > // Type of the random number
191 class Rand
192 {
193  public:
194  //**Generate functions**************************************************************************
197  inline T generate() const;
198  inline T generate( T min, T max ) const;
200  //**********************************************************************************************
201 
202  //**Randomize functions*************************************************************************
205  inline void randomize( T& value ) const;
206  inline void randomize( T& value, T min, T max ) const;
208  //**********************************************************************************************
209 };
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
221 template< typename T > // Type of the random number
222 inline T Rand<T>::generate() const
223 {
224  boost::uniform_int<T> dist( 0, std::numeric_limits<T>::max() );
225  return dist( Random<RNG>::rng_ );
226 }
227 //*************************************************************************************************
228 
229 
230 //*************************************************************************************************
242 template< typename T > // Type of the random number
243 inline T Rand<T>::generate( T min, T max ) const
244 {
245  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
246  boost::uniform_smallint<T> dist( min, max );
247  return dist( Random<RNG>::rng_ );
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
261 template< typename T > // Type of the random number
262 inline void Rand<T>::randomize( T& value ) const
263 {
264  value = generate();
265 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
282 template< typename T > // Type of the random number
283 inline void Rand<T>::randomize( T& value, T min, T max ) const
284 {
285  value = generate( min, max );
286 }
287 //*************************************************************************************************
288 
289 
290 
291 
292 //=================================================================================================
293 //
294 // RAND SPECIALIZATION (FLOAT)
295 //
296 //=================================================================================================
297 
298 //*************************************************************************************************
306 template<>
307 class Rand<float>
308 {
309  public:
310  //**Generate functions**************************************************************************
313  inline float generate() const;
314  inline float generate( float min, float max ) const;
316  //**********************************************************************************************
317 
318  //**Randomize functions*************************************************************************
321  inline void randomize( float& value ) const;
322  inline void randomize( float& value, float min, float max ) const;
324  //**********************************************************************************************
325 };
327 //*************************************************************************************************
328 
329 
330 //*************************************************************************************************
338 inline float Rand<float>::generate() const
339 {
340  boost::uniform_real<float> dist( 0.0, 1.0 );
341  return dist( Random<RNG>::rng_ );
342 }
344 //*************************************************************************************************
345 
346 
347 //*************************************************************************************************
360 inline float Rand<float>::generate( float min, float max ) const
361 {
362  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
363  boost::uniform_real<float> dist( min, max );
364  return dist( Random<RNG>::rng_ );
365 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
379 inline void Rand<float>::randomize( float& value ) const
380 {
381  value = generate();
382 }
384 //*************************************************************************************************
385 
386 
387 //*************************************************************************************************
401 inline void Rand<float>::randomize( float& value, float min, float max ) const
402 {
403  value = generate( min, max );
404 }
406 //*************************************************************************************************
407 
408 
409 
410 
411 //=================================================================================================
412 //
413 // RAND SPECIALIZATION (DOUBLE)
414 //
415 //=================================================================================================
416 
417 //*************************************************************************************************
425 template<>
426 class Rand<double>
427 {
428  public:
429  //**Generate functions**************************************************************************
432  inline double generate() const;
433  inline double generate( double min, double max ) const;
435  //**********************************************************************************************
436 
437  //**Randomize functions*************************************************************************
440  inline void randomize( double& value ) const;
441  inline void randomize( double& value, double min, double max ) const;
443  //**********************************************************************************************
444 };
446 //*************************************************************************************************
447 
448 
449 //*************************************************************************************************
457 inline double Rand<double>::generate() const
458 {
459  boost::uniform_real<double> dist( 0.0, 1.0 );
460  return dist( Random<RNG>::rng_ );
461 }
463 //*************************************************************************************************
464 
465 
466 //*************************************************************************************************
479 inline double Rand<double>::generate( double min, double max ) const
480 {
481  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
482  boost::uniform_real<double> dist( min, max );
483  return dist( Random<RNG>::rng_ );
484 }
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
498 inline void Rand<double>::randomize( double& value ) const
499 {
500  value = generate();
501 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
520 inline void Rand<double>::randomize( double& value, double min, double max ) const
521 {
522  value = generate( min, max );
523 }
525 //*************************************************************************************************
526 
527 
528 
529 
530 //=================================================================================================
531 //
532 // RAND SPECIALIZATION (LONG DOUBLE)
533 //
534 //=================================================================================================
535 
536 //*************************************************************************************************
544 template<>
545 class Rand<long double>
546 {
547  public:
548  //**Generate functions**************************************************************************
551  inline long double generate() const;
552  inline long double generate( long double min, long double max ) const;
554  //**********************************************************************************************
555 
556  //**Randomize functions*************************************************************************
559  inline void randomize( long double& value ) const;
560  inline void randomize( long double& value, long double min, long double max ) const;
562  //**********************************************************************************************
563 };
565 //*************************************************************************************************
566 
567 
568 //*************************************************************************************************
576 inline long double Rand<long double>::generate() const
577 {
578  boost::uniform_real<long double> dist( 0.0, 1.0 );
579  return dist( Random<RNG>::rng_ );
580 }
582 //*************************************************************************************************
583 
584 
585 //*************************************************************************************************
598 inline long double Rand<long double>::generate( long double min, long double max ) const
599 {
600  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
601  boost::uniform_real<long double> dist( min, max );
602  return dist( Random<RNG>::rng_ );
603 }
605 //*************************************************************************************************
606 
607 
608 //*************************************************************************************************
617 inline void Rand<long double>::randomize( long double& value ) const
618 {
619  value = generate();
620 }
622 //*************************************************************************************************
623 
624 
625 //*************************************************************************************************
639 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
640 {
641  value = generate( min, max );
642 }
644 //*************************************************************************************************
645 
646 
647 
648 
649 //=================================================================================================
650 //
651 // RAND SPECIALIZATION (COMPLEX)
652 //
653 //=================================================================================================
654 
655 //*************************************************************************************************
662 template< typename T > // Type of the values
663 class Rand< complex<T> >
664 {
665  public:
666  //**Generate functions**************************************************************************
669  inline const complex<T> generate() const;
670  inline const complex<T> generate( const T& min, const T& max ) const;
671  inline const complex<T> generate( const T& realmin, const T& realmax,
672  const T& imagmin, const T& imagmax ) const;
674  //**********************************************************************************************
675 
676  //**Randomize functions*************************************************************************
679  inline void randomize( complex<T>& value ) const;
680  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
681  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
682  const T& imagmin, const T& imagmax ) const;
684  //**********************************************************************************************
685 };
687 //*************************************************************************************************
688 
689 
690 //*************************************************************************************************
699 template< typename T > // Type of the values
700 inline const complex<T> Rand< complex<T> >::generate() const
701 {
702  Rand<T> tmp;
703  return complex<T>( tmp.generate(), tmp.generate() );
704 }
706 //*************************************************************************************************
707 
708 
709 //*************************************************************************************************
723 template< typename T > // Type of the values
724 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
725 {
726  Rand<T> tmp;
727  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
728 }
730 //*************************************************************************************************
731 
732 
733 //*************************************************************************************************
750 template< typename T > // Type of the values
751 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
752  const T& imagmin, const T& imagmax ) const
753 {
754  Rand<T> tmp;
755  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
756 }
758 //*************************************************************************************************
759 
760 
761 //*************************************************************************************************
771 template< typename T > // Type of the values
772 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
773 {
774  value = generate();
775 }
777 //*************************************************************************************************
778 
779 
780 //*************************************************************************************************
795 template< typename T > // Type of the values
796 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
797 {
798  value = generate( min, max );
799 }
801 //*************************************************************************************************
802 
803 
804 //*************************************************************************************************
823 template< typename T > // Type of the values
824 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
825  const T& imagmin, const T& imagmax ) const
826 {
827  value = generate( realmin, realmax, imagmin, imagmax );
828 }
830 //*************************************************************************************************
831 
832 
833 
834 
835 //=================================================================================================
836 //
837 // RANDOM NUMBER FUNCTIONS
838 //
839 //=================================================================================================
840 
841 //*************************************************************************************************
844 template< typename T >
845 inline T rand();
846 
847 template< typename T, typename A1 >
848 inline T rand( const A1& a1 );
849 
850 template< typename T, typename A1, typename A2 >
851 inline T rand( const A1& a1, const A2& a2 );
852 
853 template< typename T, typename A1, typename A2, typename A3 >
854 inline T rand( const A1& a1, const A2& a2, const A3& a3 );
855 
856 template< typename T, typename A1, typename A2, typename A3, typename A4 >
857 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
858 
859 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
860 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
861 
862 template< typename T >
863 inline void randomize( T& value );
864 
865 template< typename T, typename A1 >
866 inline void randomize( T& value, const A1& a1 );
867 
868 template< typename T, typename A1, typename A2 >
869 inline void randomize( T& value, const A1& a1, const A2& a2 );
870 
871 template< typename T, typename A1, typename A2, typename A3 >
872 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 );
873 
874 template< typename T, typename A1, typename A2, typename A3, typename A4 >
875 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
876 
877 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
878 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
879 
880 inline uint32_t getSeed();
881 inline void setSeed( uint32_t seed );
883 //*************************************************************************************************
884 
885 
886 //*************************************************************************************************
900 template< typename T > // Type of the random number
901 inline T rand()
902 {
903  Rand<T> tmp;
904  return tmp.generate();
905 }
906 //*************************************************************************************************
907 
908 
909 //*************************************************************************************************
918 template< typename T // Type of the random number
919  , typename A1 > // Type of the first argument
920 inline T rand( const A1& a1 )
921 {
922  Rand<T> tmp;
923  return tmp.generate( a1 );
924 }
925 //*************************************************************************************************
926 
927 
928 //*************************************************************************************************
938 template< typename T // Type of the random number
939  , typename A1 // Type of the first argument
940  , typename A2 > // Type of the second argument
941 inline T rand( const A1& a1, const A2& a2 )
942 {
943  Rand<T> tmp;
944  return tmp.generate( a1, a2 );
945 }
946 //*************************************************************************************************
947 
948 
949 //*************************************************************************************************
961 template< typename T // Type of the random number
962  , typename A1 // Type of the first argument
963  , typename A2 // Type of the second argument
964  , typename A3 > // Type of the third argument
965 inline T rand( const A1& a1, const A2& a2, const A3& a3 )
966 {
967  Rand<T> tmp;
968  return tmp.generate( a1, a2, a3 );
969 }
970 //*************************************************************************************************
971 
972 
973 //*************************************************************************************************
986 template< typename T // Type of the random number
987  , typename A1 // Type of the first argument
988  , typename A2 // Type of the second argument
989  , typename A3 // Type of the third argument
990  , typename A4 > // Type of the fourth argument
991 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
992 {
993  Rand<T> tmp;
994  return tmp.generate( a1, a2, a3, a4 );
995 }
996 //*************************************************************************************************
997 
998 
999 //*************************************************************************************************
1013 template< typename T // Type of the random number
1014  , typename A1 // Type of the first argument
1015  , typename A2 // Type of the second argument
1016  , typename A3 // Type of the third argument
1017  , typename A4 // Type of the fourth argument
1018  , typename A5 > // Type of the fifth argument
1019 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1020 {
1021  Rand<T> tmp;
1022  return tmp.generate( a1, a2, a3, a4, a5 );
1023 }
1024 //*************************************************************************************************
1025 
1026 
1027 //*************************************************************************************************
1042 template< typename T > // Type of the random number
1043 inline void randomize( T& value )
1044 {
1045  Rand<T> tmp;
1046  tmp.randomize( value );
1047 }
1048 //*************************************************************************************************
1049 
1050 
1051 //*************************************************************************************************
1061 template< typename T // Type of the random number
1062  , typename A1 > // Type of the first argument
1063 inline void randomize( T& value, const A1& a1 )
1064 {
1065  Rand<T> tmp;
1066  tmp.randomize( value, a1 );
1067 }
1068 //*************************************************************************************************
1069 
1070 
1071 //*************************************************************************************************
1083 template< typename T // Type of the random number
1084  , typename A1 // Type of the first argument
1085  , typename A2 > // Type of the second argument
1086 inline void randomize( T& value, const A1& a1, const A2& a2 )
1087 {
1088  Rand<T> tmp;
1089  tmp.randomize( value, a1, a2 );
1090 }
1091 //*************************************************************************************************
1092 
1093 
1094 //*************************************************************************************************
1107 template< typename T // Type of the random number
1108  , typename A1 // Type of the first argument
1109  , typename A2 // Type of the second argument
1110  , typename A3 > // Type of the third argument
1111 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 )
1112 {
1113  Rand<T> tmp;
1114  tmp.randomize( value, a1, a2, a3 );
1115 }
1116 //*************************************************************************************************
1117 
1118 
1119 //*************************************************************************************************
1133 template< typename T // Type of the random number
1134  , typename A1 // Type of the first argument
1135  , typename A2 // Type of the second argument
1136  , typename A3 // Type of the third argument
1137  , typename A4 > // Type of the fourth argument
1138 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
1139 {
1140  Rand<T> tmp;
1141  tmp.randomize( value, a1, a2, a3, a4 );
1142 }
1143 //*************************************************************************************************
1144 
1145 
1146 //*************************************************************************************************
1161 template< typename T // Type of the random number
1162  , typename A1 // Type of the first argument
1163  , typename A2 // Type of the second argument
1164  , typename A3 // Type of the third argument
1165  , typename A4 // Type of the fourth argument
1166  , typename A5 > // Type of the fifth argument
1167 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1168 {
1169  Rand<T> tmp;
1170  tmp.randomize( value, a1, a2, a3, a4, a5 );
1171 }
1172 //*************************************************************************************************
1173 
1174 
1175 //*************************************************************************************************
1182 {
1183  return Random<RNG>::seed_;
1184 }
1185 //*************************************************************************************************
1186 
1187 
1188 //*************************************************************************************************
1198 inline void setSeed( uint32_t seed )
1199 {
1200  Random<RNG>::seed_ = seed;
1201  Random<RNG>::rng_.seed( seed );
1202 }
1203 //*************************************************************************************************
1204 
1205 } // namespace blaze
1206 
1207 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1649
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:1181
Header file for basic type definitions.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1043
32-bit unsigned integer type of the Blaze library.
Header file for the random number generator used in the Blaze library.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:262
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:1198
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1602
T rand()
Random number function.
Definition: Random.h:901
Header file for run time assertion macros.
Default implementation of the Rand class for integral data types.This default implementation of the R...
Definition: Random.h:63
Random number generator.The Random class encapsulates the initialization of the given random number g...
Definition: Random.h:139
static uint32_t seed_
The current seed for the variate generator.
Definition: Random.h:145
static Type rng_
The mersenne twister variate generator.
Definition: Random.h:146
T generate() const
Generation of a random value in the range .
Definition: Random.h:222
Header file for the complex data type.
Base class for non-creatable (static) classes.The NonCreatable class is intended to work as a base cl...
Definition: NonCreatable.h:65
Base class for non-creatable (static) classes.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101