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  friend uint32_t getSeed();
154  friend void setSeed( uint32_t seed );
156  //**********************************************************************************************
157 };
158 //*************************************************************************************************
159 
160 
161 
162 
163 //=================================================================================================
164 //
165 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
166 //
167 //=================================================================================================
168 
169 template< typename Type > uint32_t Random<Type>::seed_( static_cast<uint32_t>( std::time(0) ) );
170 template< typename Type > Type Random<Type>::rng_ ( seed_ );
171 
172 
173 
174 
175 //=================================================================================================
176 //
177 // CLASS RAND
178 //
179 //=================================================================================================
180 
181 //*************************************************************************************************
188 template< typename T > // Type of the random number
189 class Rand
190 {
191  public:
192  //**Generate functions**************************************************************************
195  inline T generate() const;
196  inline T generate( T min, T max ) const;
198  //**********************************************************************************************
199 
200  //**Randomize functions*************************************************************************
203  inline void randomize( T& value ) const;
204  inline void randomize( T& value, T min, T max ) const;
206  //**********************************************************************************************
207 };
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
219 template< typename T > // Type of the random number
220 inline T Rand<T>::generate() const
221 {
222  boost::uniform_int<T> dist( 0, std::numeric_limits<T>::max() );
223  return dist( Random<RNG>::rng_ );
224 }
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
240 template< typename T > // Type of the random number
241 inline T Rand<T>::generate( T min, T max ) const
242 {
243  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
244  boost::uniform_smallint<T> dist( min, max );
245  return dist( Random<RNG>::rng_ );
246 }
247 //*************************************************************************************************
248 
249 
250 //*************************************************************************************************
259 template< typename T > // Type of the random number
260 inline void Rand<T>::randomize( T& value ) const
261 {
262  value = generate();
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
280 template< typename T > // Type of the random number
281 inline void Rand<T>::randomize( T& value, T min, T max ) const
282 {
283  value = generate( min, max );
284 }
285 //*************************************************************************************************
286 
287 
288 
289 
290 //=================================================================================================
291 //
292 // RAND SPECIALIZATION (FLOAT)
293 //
294 //=================================================================================================
295 
296 //*************************************************************************************************
304 template<>
305 class Rand<float>
306 {
307  public:
308  //**Generate functions**************************************************************************
311  inline float generate() const;
312  inline float generate( float min, float max ) const;
314  //**********************************************************************************************
315 
316  //**Randomize functions*************************************************************************
319  inline void randomize( float& value ) const;
320  inline void randomize( float& value, float min, float max ) const;
322  //**********************************************************************************************
323 };
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
336 inline float Rand<float>::generate() const
337 {
338  boost::uniform_real<float> dist( 0.0, 1.0 );
339  return dist( Random<RNG>::rng_ );
340 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
358 inline float Rand<float>::generate( float min, float max ) const
359 {
360  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
361  boost::uniform_real<float> dist( min, max );
362  return dist( Random<RNG>::rng_ );
363 }
365 //*************************************************************************************************
366 
367 
368 //*************************************************************************************************
377 inline void Rand<float>::randomize( float& value ) const
378 {
379  value = generate();
380 }
382 //*************************************************************************************************
383 
384 
385 //*************************************************************************************************
399 inline void Rand<float>::randomize( float& value, float min, float max ) const
400 {
401  value = generate( min, max );
402 }
404 //*************************************************************************************************
405 
406 
407 
408 
409 //=================================================================================================
410 //
411 // RAND SPECIALIZATION (DOUBLE)
412 //
413 //=================================================================================================
414 
415 //*************************************************************************************************
423 template<>
424 class Rand<double>
425 {
426  public:
427  //**Generate functions**************************************************************************
430  inline double generate() const;
431  inline double generate( double min, double max ) const;
433  //**********************************************************************************************
434 
435  //**Randomize functions*************************************************************************
438  inline void randomize( double& value ) const;
439  inline void randomize( double& value, double min, double max ) const;
441  //**********************************************************************************************
442 };
444 //*************************************************************************************************
445 
446 
447 //*************************************************************************************************
455 inline double Rand<double>::generate() const
456 {
457  boost::uniform_real<double> dist( 0.0, 1.0 );
458  return dist( Random<RNG>::rng_ );
459 }
461 //*************************************************************************************************
462 
463 
464 //*************************************************************************************************
477 inline double Rand<double>::generate( double min, double max ) const
478 {
479  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
480  boost::uniform_real<double> dist( min, max );
481  return dist( Random<RNG>::rng_ );
482 }
484 //*************************************************************************************************
485 
486 
487 //*************************************************************************************************
496 inline void Rand<double>::randomize( double& value ) const
497 {
498  value = generate();
499 }
501 //*************************************************************************************************
502 
503 
504 //*************************************************************************************************
518 inline void Rand<double>::randomize( double& value, double min, double max ) const
519 {
520  value = generate( min, max );
521 }
523 //*************************************************************************************************
524 
525 
526 
527 
528 //=================================================================================================
529 //
530 // RAND SPECIALIZATION (LONG DOUBLE)
531 //
532 //=================================================================================================
533 
534 //*************************************************************************************************
542 template<>
543 class Rand<long double>
544 {
545  public:
546  //**Generate functions**************************************************************************
549  inline long double generate() const;
550  inline long double generate( long double min, long double max ) const;
552  //**********************************************************************************************
553 
554  //**Randomize functions*************************************************************************
557  inline void randomize( long double& value ) const;
558  inline void randomize( long double& value, long double min, long double max ) const;
560  //**********************************************************************************************
561 };
563 //*************************************************************************************************
564 
565 
566 //*************************************************************************************************
574 inline long double Rand<long double>::generate() const
575 {
576  boost::uniform_real<long double> dist( 0.0, 1.0 );
577  return dist( Random<RNG>::rng_ );
578 }
580 //*************************************************************************************************
581 
582 
583 //*************************************************************************************************
596 inline long double Rand<long double>::generate( long double min, long double max ) const
597 {
598  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
599  boost::uniform_real<long double> dist( min, max );
600  return dist( Random<RNG>::rng_ );
601 }
603 //*************************************************************************************************
604 
605 
606 //*************************************************************************************************
615 inline void Rand<long double>::randomize( long double& value ) const
616 {
617  value = generate();
618 }
620 //*************************************************************************************************
621 
622 
623 //*************************************************************************************************
637 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
638 {
639  value = generate( min, max );
640 }
642 //*************************************************************************************************
643 
644 
645 
646 
647 //=================================================================================================
648 //
649 // RAND SPECIALIZATION (COMPLEX)
650 //
651 //=================================================================================================
652 
653 //*************************************************************************************************
660 template< typename T > // Type of the values
661 class Rand< complex<T> >
662 {
663  public:
664  //**Generate functions**************************************************************************
667  inline const complex<T> generate() const;
668  inline const complex<T> generate( const T& min, const T& max ) const;
669  inline const complex<T> generate( const T& realmin, const T& realmax,
670  const T& imagmin, const T& imagmax ) const;
672  //**********************************************************************************************
673 
674  //**Randomize functions*************************************************************************
677  inline void randomize( complex<T>& value ) const;
678  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
679  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
680  const T& imagmin, const T& imagmax ) const;
682  //**********************************************************************************************
683 };
685 //*************************************************************************************************
686 
687 
688 //*************************************************************************************************
697 template< typename T > // Type of the values
698 inline const complex<T> Rand< complex<T> >::generate() const
699 {
700  Rand<T> tmp;
701  return complex<T>( tmp.generate(), tmp.generate() );
702 }
704 //*************************************************************************************************
705 
706 
707 //*************************************************************************************************
721 template< typename T > // Type of the values
722 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
723 {
724  Rand<T> tmp;
725  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
726 }
728 //*************************************************************************************************
729 
730 
731 //*************************************************************************************************
748 template< typename T > // Type of the values
749 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
750  const T& imagmin, const T& imagmax ) const
751 {
752  Rand<T> tmp;
753  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
754 }
756 //*************************************************************************************************
757 
758 
759 //*************************************************************************************************
769 template< typename T > // Type of the values
770 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
771 {
772  value = generate();
773 }
775 //*************************************************************************************************
776 
777 
778 //*************************************************************************************************
793 template< typename T > // Type of the values
794 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
795 {
796  value = generate( min, max );
797 }
799 //*************************************************************************************************
800 
801 
802 //*************************************************************************************************
821 template< typename T > // Type of the values
822 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
823  const T& imagmin, const T& imagmax ) const
824 {
825  value = generate( realmin, realmax, imagmin, imagmax );
826 }
828 //*************************************************************************************************
829 
830 
831 
832 
833 //=================================================================================================
834 //
835 // RANDOM NUMBER FUNCTIONS
836 //
837 //=================================================================================================
838 
839 //*************************************************************************************************
842 template< typename T >
843 inline T rand();
844 
845 template< typename T, typename A1 >
846 inline T rand( const A1& a1 );
847 
848 template< typename T, typename A1, typename A2 >
849 inline T rand( const A1& a1, const A2& a2 );
850 
851 template< typename T, typename A1, typename A2, typename A3 >
852 inline T rand( const A1& a1, const A2& a2, const A3& a3 );
853 
854 template< typename T, typename A1, typename A2, typename A3, typename A4 >
855 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
856 
857 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
858 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
859 
860 template< typename T >
861 inline void randomize( T& value );
862 
863 template< typename T, typename A1 >
864 inline void randomize( T& value, const A1& a1 );
865 
866 template< typename T, typename A1, typename A2 >
867 inline void randomize( T& value, const A1& a1, const A2& a2 );
868 
869 template< typename T, typename A1, typename A2, typename A3 >
870 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 );
871 
872 template< typename T, typename A1, typename A2, typename A3, typename A4 >
873 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
874 
875 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
876 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
877 
878 inline uint32_t getSeed();
879 inline void setSeed( uint32_t seed );
881 //*************************************************************************************************
882 
883 
884 //*************************************************************************************************
898 template< typename T > // Type of the random number
899 inline T rand()
900 {
901  Rand<T> tmp;
902  return tmp.generate();
903 }
904 //*************************************************************************************************
905 
906 
907 //*************************************************************************************************
916 template< typename T // Type of the random number
917  , typename A1 > // Type of the first argument
918 inline T rand( const A1& a1 )
919 {
920  Rand<T> tmp;
921  return tmp.generate( a1 );
922 }
923 //*************************************************************************************************
924 
925 
926 //*************************************************************************************************
936 template< typename T // Type of the random number
937  , typename A1 // Type of the first argument
938  , typename A2 > // Type of the second argument
939 inline T rand( const A1& a1, const A2& a2 )
940 {
941  Rand<T> tmp;
942  return tmp.generate( a1, a2 );
943 }
944 //*************************************************************************************************
945 
946 
947 //*************************************************************************************************
959 template< typename T // Type of the random number
960  , typename A1 // Type of the first argument
961  , typename A2 // Type of the second argument
962  , typename A3 > // Type of the third argument
963 inline T rand( const A1& a1, const A2& a2, const A3& a3 )
964 {
965  Rand<T> tmp;
966  return tmp.generate( a1, a2, a3 );
967 }
968 //*************************************************************************************************
969 
970 
971 //*************************************************************************************************
984 template< typename T // Type of the random number
985  , typename A1 // Type of the first argument
986  , typename A2 // Type of the second argument
987  , typename A3 // Type of the third argument
988  , typename A4 > // Type of the fourth argument
989 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
990 {
991  Rand<T> tmp;
992  return tmp.generate( a1, a2, a3, a4 );
993 }
994 //*************************************************************************************************
995 
996 
997 //*************************************************************************************************
1011 template< typename T // Type of the random number
1012  , typename A1 // Type of the first argument
1013  , typename A2 // Type of the second argument
1014  , typename A3 // Type of the third argument
1015  , typename A4 // Type of the fourth argument
1016  , typename A5 > // Type of the fifth argument
1017 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1018 {
1019  Rand<T> tmp;
1020  return tmp.generate( a1, a2, a3, a4, a5 );
1021 }
1022 //*************************************************************************************************
1023 
1024 
1025 //*************************************************************************************************
1040 template< typename T > // Type of the random number
1041 inline void randomize( T& value )
1042 {
1043  Rand<T> tmp;
1044  tmp.randomize( value );
1045 }
1046 //*************************************************************************************************
1047 
1048 
1049 //*************************************************************************************************
1059 template< typename T // Type of the random number
1060  , typename A1 > // Type of the first argument
1061 inline void randomize( T& value, const A1& a1 )
1062 {
1063  Rand<T> tmp;
1064  tmp.randomize( value, a1 );
1065 }
1066 //*************************************************************************************************
1067 
1068 
1069 //*************************************************************************************************
1081 template< typename T // Type of the random number
1082  , typename A1 // Type of the first argument
1083  , typename A2 > // Type of the second argument
1084 inline void randomize( T& value, const A1& a1, const A2& a2 )
1085 {
1086  Rand<T> tmp;
1087  tmp.randomize( value, a1, a2 );
1088 }
1089 //*************************************************************************************************
1090 
1091 
1092 //*************************************************************************************************
1105 template< typename T // Type of the random number
1106  , typename A1 // Type of the first argument
1107  , typename A2 // Type of the second argument
1108  , typename A3 > // Type of the third argument
1109 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 )
1110 {
1111  Rand<T> tmp;
1112  tmp.randomize( value, a1, a2, a3 );
1113 }
1114 //*************************************************************************************************
1115 
1116 
1117 //*************************************************************************************************
1131 template< typename T // Type of the random number
1132  , typename A1 // Type of the first argument
1133  , typename A2 // Type of the second argument
1134  , typename A3 // Type of the third argument
1135  , typename A4 > // Type of the fourth argument
1136 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
1137 {
1138  Rand<T> tmp;
1139  tmp.randomize( value, a1, a2, a3, a4 );
1140 }
1141 //*************************************************************************************************
1142 
1143 
1144 //*************************************************************************************************
1159 template< typename T // Type of the random number
1160  , typename A1 // Type of the first argument
1161  , typename A2 // Type of the second argument
1162  , typename A3 // Type of the third argument
1163  , typename A4 // Type of the fourth argument
1164  , typename A5 > // Type of the fifth argument
1165 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1166 {
1167  Rand<T> tmp;
1168  tmp.randomize( value, a1, a2, a3, a4, a5 );
1169 }
1170 //*************************************************************************************************
1171 
1172 
1173 //*************************************************************************************************
1180 {
1181  return Random<RNG>::seed_;
1182 }
1183 //*************************************************************************************************
1184 
1185 
1186 //*************************************************************************************************
1196 inline void setSeed( uint32_t seed )
1197 {
1198  Random<RNG>::seed_ = seed;
1199  Random<RNG>::rng_.seed( seed );
1200 }
1201 //*************************************************************************************************
1202 
1203 } // namespace blaze
1204 
1205 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:1179
Header file for basic type definitions.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
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:260
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:1196
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
T rand()
Random number function.
Definition: Random.h:899
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:220
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