All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Random.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_RANDOM_H_
23 #define _BLAZE_UTIL_RANDOM_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <ctime>
31 #include <limits>
32 #include <boost/random/uniform_int.hpp>
33 #include <boost/random/uniform_real.hpp>
34 #include <boost/random/uniform_smallint.hpp>
35 #include <blaze/system/Random.h>
36 #include <blaze/util/Assert.h>
37 #include <blaze/util/Complex.h>
39 #include <blaze/util/Types.h>
40 
41 
42 namespace blaze {
43 
44 //=================================================================================================
45 //
46 // ::blaze NAMESPACE FORWARD DECLARATIONS
47 //
48 //=================================================================================================
49 
50 template< typename > class Rand;
51 
52 
53 
54 
55 //=================================================================================================
56 //
57 // CLASS DEFINITION
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
125 template< typename Type > // Type of the random number generator
126 class Random : private NonCreatable
127 {
128  private:
129  //**Member variables****************************************************************************
132  static uint32_t seed_;
133  static Type rng_;
134 
135  //**********************************************************************************************
136 
137  //**Friend declarations*************************************************************************
139  template< typename T > friend class Rand;
140 // template< typename T > friend T rand();
141 // template< typename T > friend T rand( T min, T max );
142  friend uint32_t getSeed();
143  friend void setSeed( uint32_t seed );
145  //**********************************************************************************************
146 };
147 //*************************************************************************************************
148 
149 
150 
151 
152 //=================================================================================================
153 //
154 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
155 //
156 //=================================================================================================
157 
158 template< typename Type > uint32_t Random<Type>::seed_( static_cast<uint32_t>( std::time(0) ) );
159 template< typename Type > Type Random<Type>::rng_ ( seed_ );
160 
161 
162 
163 
164 //=================================================================================================
165 //
166 // CLASS RAND
167 //
168 //=================================================================================================
169 
170 //*************************************************************************************************
177 template< typename T > // Type of the random number
178 class Rand
179 {
180  public:
181  //**Generate functions**************************************************************************
184  inline T generate() const;
185  inline T generate( T min, T max ) const;
187  //**********************************************************************************************
188 
189  //**Randomize functions*************************************************************************
192  inline void randomize( T& value ) const;
193  inline void randomize( T& value, T min, T max ) const;
195  //**********************************************************************************************
196 };
197 //*************************************************************************************************
198 
199 
200 //*************************************************************************************************
208 template< typename T > // Type of the random number
209 inline T Rand<T>::generate() const
210 {
211  boost::uniform_int<T> dist( 0, std::numeric_limits<T>::max() );
212  return dist( Random<RNG>::rng_ );
213 }
214 //*************************************************************************************************
215 
216 
217 //*************************************************************************************************
229 template< typename T > // Type of the random number
230 inline T Rand<T>::generate( T min, T max ) const
231 {
232  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
233  boost::uniform_smallint<T> dist( min, max );
234  return dist( Random<RNG>::rng_ );
235 }
236 //*************************************************************************************************
237 
238 
239 //*************************************************************************************************
248 template< typename T > // Type of the random number
249 inline void Rand<T>::randomize( T& value ) const
250 {
251  value = generate();
252 }
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
269 template< typename T > // Type of the random number
270 inline void Rand<T>::randomize( T& value, T min, T max ) const
271 {
272  value = generate( min, max );
273 }
274 //*************************************************************************************************
275 
276 
277 
278 
279 //=================================================================================================
280 //
281 // RAND SPECIALIZATION (FLOAT)
282 //
283 //=================================================================================================
284 
285 //*************************************************************************************************
293 template<>
294 class Rand<float>
295 {
296  public:
297  //**Generate functions**************************************************************************
300  inline float generate() const;
301  inline float generate( float min, float max ) const;
303  //**********************************************************************************************
304 
305  //**Randomize functions*************************************************************************
308  inline void randomize( float& value ) const;
309  inline void randomize( float& value, float min, float max ) const;
311  //**********************************************************************************************
312 };
314 //*************************************************************************************************
315 
316 
317 //*************************************************************************************************
325 inline float Rand<float>::generate() const
326 {
327  boost::uniform_real<float> dist( 0.0, 1.0 );
328  return dist( Random<RNG>::rng_ );
329 }
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
347 inline float Rand<float>::generate( float min, float max ) const
348 {
349  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
350  boost::uniform_real<float> dist( min, max );
351  return dist( Random<RNG>::rng_ );
352 }
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
366 inline void Rand<float>::randomize( float& value ) const
367 {
368  value = generate();
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
388 inline void Rand<float>::randomize( float& value, float min, float max ) const
389 {
390  value = generate( min, max );
391 }
393 //*************************************************************************************************
394 
395 
396 
397 
398 //=================================================================================================
399 //
400 // RAND SPECIALIZATION (DOUBLE)
401 //
402 //=================================================================================================
403 
404 //*************************************************************************************************
412 template<>
413 class Rand<double>
414 {
415  public:
416  //**Generate functions**************************************************************************
419  inline double generate() const;
420  inline double generate( double min, double max ) const;
422  //**********************************************************************************************
423 
424  //**Randomize functions*************************************************************************
427  inline void randomize( double& value ) const;
428  inline void randomize( double& value, double min, double max ) const;
430  //**********************************************************************************************
431 };
433 //*************************************************************************************************
434 
435 
436 //*************************************************************************************************
444 inline double Rand<double>::generate() const
445 {
446  boost::uniform_real<double> dist( 0.0, 1.0 );
447  return dist( Random<RNG>::rng_ );
448 }
450 //*************************************************************************************************
451 
452 
453 //*************************************************************************************************
466 inline double Rand<double>::generate( double min, double max ) const
467 {
468  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
469  boost::uniform_real<double> dist( min, max );
470  return dist( Random<RNG>::rng_ );
471 }
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
485 inline void Rand<double>::randomize( double& value ) const
486 {
487  value = generate();
488 }
490 //*************************************************************************************************
491 
492 
493 //*************************************************************************************************
507 inline void Rand<double>::randomize( double& value, double min, double max ) const
508 {
509  value = generate( min, max );
510 }
512 //*************************************************************************************************
513 
514 
515 
516 
517 //=================================================================================================
518 //
519 // RAND SPECIALIZATION (LONG DOUBLE)
520 //
521 //=================================================================================================
522 
523 //*************************************************************************************************
531 template<>
532 class Rand<long double>
533 {
534  public:
535  //**Generate functions**************************************************************************
538  inline long double generate() const;
539  inline long double generate( long double min, long double max ) const;
541  //**********************************************************************************************
542 
543  //**Randomize functions*************************************************************************
546  inline void randomize( long double& value ) const;
547  inline void randomize( long double& value, long double min, long double max ) const;
549  //**********************************************************************************************
550 };
552 //*************************************************************************************************
553 
554 
555 //*************************************************************************************************
563 inline long double Rand<long double>::generate() const
564 {
565  boost::uniform_real<long double> dist( 0.0, 1.0 );
566  return dist( Random<RNG>::rng_ );
567 }
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
585 inline long double Rand<long double>::generate( long double min, long double max ) const
586 {
587  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
588  boost::uniform_real<long double> dist( min, max );
589  return dist( Random<RNG>::rng_ );
590 }
592 //*************************************************************************************************
593 
594 
595 //*************************************************************************************************
604 inline void Rand<long double>::randomize( long double& value ) const
605 {
606  value = generate();
607 }
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
626 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
627 {
628  value = generate( min, max );
629 }
631 //*************************************************************************************************
632 
633 
634 
635 
636 //=================================================================================================
637 //
638 // RAND SPECIALIZATION (COMPLEX)
639 //
640 //=================================================================================================
641 
642 //*************************************************************************************************
649 template< typename T > // Type of the values
650 class Rand< complex<T> >
651 {
652  public:
653  //**Generate functions**************************************************************************
656  inline const complex<T> generate() const;
657  inline const complex<T> generate( const T& min, const T& max ) const;
658  inline const complex<T> generate( const T& realmin, const T& realmax,
659  const T& imagmin, const T& imagmax ) const;
661  //**********************************************************************************************
662 
663  //**Randomize functions*************************************************************************
666  inline void randomize( complex<T>& value ) const;
667  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
668  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
669  const T& imagmin, const T& imagmax ) const;
671  //**********************************************************************************************
672 };
674 //*************************************************************************************************
675 
676 
677 //*************************************************************************************************
686 template< typename T > // Type of the values
687 inline const complex<T> Rand< complex<T> >::generate() const
688 {
689  Rand<T> tmp;
690  return complex<T>( tmp.generate(), tmp.generate() );
691 }
693 //*************************************************************************************************
694 
695 
696 //*************************************************************************************************
710 template< typename T > // Type of the values
711 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
712 {
713  Rand<T> tmp;
714  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
715 }
717 //*************************************************************************************************
718 
719 
720 //*************************************************************************************************
737 template< typename T > // Type of the values
738 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
739  const T& imagmin, const T& imagmax ) const
740 {
741  Rand<T> tmp;
742  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
743 }
745 //*************************************************************************************************
746 
747 
748 //*************************************************************************************************
758 template< typename T > // Type of the values
759 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
760 {
761  value = generate();
762 }
764 //*************************************************************************************************
765 
766 
767 //*************************************************************************************************
782 template< typename T > // Type of the values
783 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
784 {
785  value = generate( min, max );
786 }
788 //*************************************************************************************************
789 
790 
791 //*************************************************************************************************
810 template< typename T > // Type of the values
811 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
812  const T& imagmin, const T& imagmax ) const
813 {
814  value = generate( realmin, realmax, imagmin, imagmax );
815 }
817 //*************************************************************************************************
818 
819 
820 
821 
822 //=================================================================================================
823 //
824 // RANDOM NUMBER FUNCTIONS
825 //
826 //=================================================================================================
827 
828 //*************************************************************************************************
831 template< typename T >
832 inline T rand();
833 
834 template< typename T, typename A1 >
835 inline T rand( const A1& a1 );
836 
837 template< typename T, typename A1, typename A2 >
838 inline T rand( const A1& a1, const A2& a2 );
839 
840 template< typename T, typename A1, typename A2, typename A3 >
841 inline T rand( const A1& a1, const A2& a2, const A3& a3 );
842 
843 template< typename T, typename A1, typename A2, typename A3, typename A4 >
844 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
845 
846 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
847 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
848 
849 template< typename T >
850 inline void randomize( T& value );
851 
852 template< typename T, typename A1 >
853 inline void randomize( T& value, const A1& a1 );
854 
855 template< typename T, typename A1, typename A2 >
856 inline void randomize( T& value, const A1& a1, const A2& a2 );
857 
858 template< typename T, typename A1, typename A2, typename A3 >
859 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 );
860 
861 template< typename T, typename A1, typename A2, typename A3, typename A4 >
862 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
863 
864 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
865 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
866 
867 inline uint32_t getSeed();
868 inline void setSeed( uint32_t seed );
870 //*************************************************************************************************
871 
872 
873 //*************************************************************************************************
887 template< typename T > // Type of the random number
888 inline T rand()
889 {
890  Rand<T> tmp;
891  return tmp.generate();
892 }
893 //*************************************************************************************************
894 
895 
896 //*************************************************************************************************
905 template< typename T // Type of the random number
906  , typename A1 > // Type of the first argument
907 inline T rand( const A1& a1 )
908 {
909  Rand<T> tmp;
910  return tmp.generate( a1 );
911 }
912 //*************************************************************************************************
913 
914 
915 //*************************************************************************************************
925 template< typename T // Type of the random number
926  , typename A1 // Type of the first argument
927  , typename A2 > // Type of the second argument
928 inline T rand( const A1& a1, const A2& a2 )
929 {
930  Rand<T> tmp;
931  return tmp.generate( a1, a2 );
932 }
933 //*************************************************************************************************
934 
935 
936 //*************************************************************************************************
948 template< typename T // Type of the random number
949  , typename A1 // Type of the first argument
950  , typename A2 // Type of the second argument
951  , typename A3 > // Type of the third argument
952 inline T rand( const A1& a1, const A2& a2, const A3& a3 )
953 {
954  Rand<T> tmp;
955  return tmp.generate( a1, a2, a3 );
956 }
957 //*************************************************************************************************
958 
959 
960 //*************************************************************************************************
973 template< typename T // Type of the random number
974  , typename A1 // Type of the first argument
975  , typename A2 // Type of the second argument
976  , typename A3 // Type of the third argument
977  , typename A4 > // Type of the fourth argument
978 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
979 {
980  Rand<T> tmp;
981  return tmp.generate( a1, a2, a3, a4 );
982 }
983 //*************************************************************************************************
984 
985 
986 //*************************************************************************************************
1000 template< typename T // Type of the random number
1001  , typename A1 // Type of the first argument
1002  , typename A2 // Type of the second argument
1003  , typename A3 // Type of the third argument
1004  , typename A4 // Type of the fourth argument
1005  , typename A5 > // Type of the fifth argument
1006 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1007 {
1008  Rand<T> tmp;
1009  return tmp.generate( a1, a2, a3, a4, a5 );
1010 }
1011 //*************************************************************************************************
1012 
1013 
1014 //*************************************************************************************************
1029 template< typename T > // Type of the random number
1030 inline void randomize( T& value )
1031 {
1032  Rand<T> tmp;
1033  tmp.randomize( value );
1034 }
1035 //*************************************************************************************************
1036 
1037 
1038 //*************************************************************************************************
1048 template< typename T // Type of the random number
1049  , typename A1 > // Type of the first argument
1050 inline void randomize( T& value, const A1& a1 )
1051 {
1052  Rand<T> tmp;
1053  tmp.randomize( value, a1 );
1054 }
1055 //*************************************************************************************************
1056 
1057 
1058 //*************************************************************************************************
1070 template< typename T // Type of the random number
1071  , typename A1 // Type of the first argument
1072  , typename A2 > // Type of the second argument
1073 inline void randomize( T& value, const A1& a1, const A2& a2 )
1074 {
1075  Rand<T> tmp;
1076  tmp.randomize( value, a1, a2 );
1077 }
1078 //*************************************************************************************************
1079 
1080 
1081 //*************************************************************************************************
1094 template< typename T // Type of the random number
1095  , typename A1 // Type of the first argument
1096  , typename A2 // Type of the second argument
1097  , typename A3 > // Type of the third argument
1098 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3 )
1099 {
1100  Rand<T> tmp;
1101  tmp.randomize( value, a1, a2, a3 );
1102 }
1103 //*************************************************************************************************
1104 
1105 
1106 //*************************************************************************************************
1120 template< typename T // Type of the random number
1121  , typename A1 // Type of the first argument
1122  , typename A2 // Type of the second argument
1123  , typename A3 // Type of the third argument
1124  , typename A4 > // Type of the fourth argument
1125 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
1126 {
1127  Rand<T> tmp;
1128  tmp.randomize( value, a1, a2, a3, a4 );
1129 }
1130 //*************************************************************************************************
1131 
1132 
1133 //*************************************************************************************************
1148 template< typename T // Type of the random number
1149  , typename A1 // Type of the first argument
1150  , typename A2 // Type of the second argument
1151  , typename A3 // Type of the third argument
1152  , typename A4 // Type of the fourth argument
1153  , typename A5 > // Type of the fifth argument
1154 inline void randomize( T& value, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
1155 {
1156  Rand<T> tmp;
1157  tmp.randomize( value, a1, a2, a3, a4, a5 );
1158 }
1159 //*************************************************************************************************
1160 
1161 
1162 //*************************************************************************************************
1169 {
1170  return Random<RNG>::seed_;
1171 }
1172 //*************************************************************************************************
1173 
1174 
1175 //*************************************************************************************************
1185 inline void setSeed( uint32_t seed )
1186 {
1187  Random<RNG>::seed_ = seed;
1188  Random<RNG>::rng_.seed( seed );
1189 }
1190 //*************************************************************************************************
1191 
1192 } // namespace blaze
1193 
1194 #endif