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 <random>
46 #include <blaze/system/Random.h>
47 #include <blaze/util/Assert.h>
48 #include <blaze/util/Complex.h>
50 #include <blaze/util/Types.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // DOXYGEN DOCUMENTATION
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
114 //*************************************************************************************************
115 
116 
117 
118 
119 //=================================================================================================
120 //
121 // ::blaze NAMESPACE FORWARD DECLARATIONS
122 //
123 //=================================================================================================
124 
125 //*************************************************************************************************
128 template< typename T >
129 inline T rand();
130 
131 template< typename T, typename... Args >
132 inline T rand( Args&&... args );
133 
134 template< typename T >
135 inline void randomize( T& value );
136 
137 template< typename T, typename... Args >
138 inline void randomize( T& value, Args&&... args );
139 
140 inline uint32_t defaultSeed();
141 inline uint32_t getSeed();
142 inline void setSeed( uint32_t seed );
144 //*************************************************************************************************
145 
146 
147 
148 
149 //=================================================================================================
150 //
151 // CLASS DEFINITION
152 //
153 //=================================================================================================
154 
155 //*************************************************************************************************
167 template< typename Type > // Type of the random number generator
168 class Random : private NonCreatable
169 {
170  private:
171  //**Member variables****************************************************************************
174  static uint32_t seed_;
175  static Type rng_;
176 
177  //**********************************************************************************************
178 
179  //**Friend declarations*************************************************************************
181  template< typename T > friend class Rand;
182  friend uint32_t getSeed();
183  friend void setSeed( uint32_t seed );
185  //**********************************************************************************************
186 };
187 //*************************************************************************************************
188 
189 
190 
191 
192 //=================================================================================================
193 //
194 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
195 //
196 //=================================================================================================
197 
198 template< typename Type > uint32_t Random<Type>::seed_( defaultSeed() );
199 template< typename Type > Type Random<Type>::rng_ ( defaultSeed() );
200 
201 
202 
203 
204 //=================================================================================================
205 //
206 // CLASS RAND
207 //
208 //=================================================================================================
209 
210 //*************************************************************************************************
217 template< typename T > // Type of the random number
218 class Rand
219 {
220  public:
221  //**Generate functions**************************************************************************
224  inline T generate() const;
225  inline T generate( T min, T max ) const;
227  //**********************************************************************************************
228 
229  //**Randomize functions*************************************************************************
232  inline void randomize( T& value ) const;
233  inline void randomize( T& value, T min, T max ) const;
235  //**********************************************************************************************
236 };
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
248 template< typename T > // Type of the random number
249 inline T Rand<T>::generate() const
250 {
251  std::uniform_int_distribution<T> dist( 0, std::numeric_limits<T>::max() );
252  return dist( Random<RNG>::rng_ );
253 }
254 //*************************************************************************************************
255 
256 
257 //*************************************************************************************************
269 template< typename T > // Type of the random number
270 inline T Rand<T>::generate( T min, T max ) const
271 {
272  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
273  std::uniform_int_distribution<T> dist( min, max );
274  return dist( Random<RNG>::rng_ );
275 }
276 //*************************************************************************************************
277 
278 
279 //*************************************************************************************************
288 template< typename T > // Type of the random number
289 inline void Rand<T>::randomize( T& value ) const
290 {
291  value = generate();
292 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
309 template< typename T > // Type of the random number
310 inline void Rand<T>::randomize( T& value, T min, T max ) const
311 {
312  value = generate( min, max );
313 }
314 //*************************************************************************************************
315 
316 
317 
318 
319 //=================================================================================================
320 //
321 // RAND SPECIALIZATION (FLOAT)
322 //
323 //=================================================================================================
324 
325 //*************************************************************************************************
333 template<>
334 class Rand<float>
335 {
336  public:
337  //**Generate functions**************************************************************************
340  inline float generate() const;
341  inline float generate( float min, float max ) const;
343  //**********************************************************************************************
344 
345  //**Randomize functions*************************************************************************
348  inline void randomize( float& value ) const;
349  inline void randomize( float& value, float min, float max ) const;
351  //**********************************************************************************************
352 };
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
365 inline float Rand<float>::generate() const
366 {
367  std::uniform_real_distribution<float> dist( 0.0, 1.0 );
368  return dist( Random<RNG>::rng_ );
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
387 inline float Rand<float>::generate( float min, float max ) const
388 {
389  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
390  std::uniform_real_distribution<float> dist( min, max );
391  return dist( Random<RNG>::rng_ );
392 }
394 //*************************************************************************************************
395 
396 
397 //*************************************************************************************************
406 inline void Rand<float>::randomize( float& value ) const
407 {
408  value = generate();
409 }
411 //*************************************************************************************************
412 
413 
414 //*************************************************************************************************
428 inline void Rand<float>::randomize( float& value, float min, float max ) const
429 {
430  value = generate( min, max );
431 }
433 //*************************************************************************************************
434 
435 
436 
437 
438 //=================================================================================================
439 //
440 // RAND SPECIALIZATION (DOUBLE)
441 //
442 //=================================================================================================
443 
444 //*************************************************************************************************
452 template<>
453 class Rand<double>
454 {
455  public:
456  //**Generate functions**************************************************************************
459  inline double generate() const;
460  inline double generate( double min, double max ) const;
462  //**********************************************************************************************
463 
464  //**Randomize functions*************************************************************************
467  inline void randomize( double& value ) const;
468  inline void randomize( double& value, double min, double max ) const;
470  //**********************************************************************************************
471 };
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
484 inline double Rand<double>::generate() const
485 {
486  std::uniform_real_distribution<double> dist( 0.0, 1.0 );
487  return dist( Random<RNG>::rng_ );
488 }
490 //*************************************************************************************************
491 
492 
493 //*************************************************************************************************
506 inline double Rand<double>::generate( double min, double max ) const
507 {
508  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
509  std::uniform_real_distribution<double> dist( min, max );
510  return dist( Random<RNG>::rng_ );
511 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
525 inline void Rand<double>::randomize( double& value ) const
526 {
527  value = generate();
528 }
530 //*************************************************************************************************
531 
532 
533 //*************************************************************************************************
547 inline void Rand<double>::randomize( double& value, double min, double max ) const
548 {
549  value = generate( min, max );
550 }
552 //*************************************************************************************************
553 
554 
555 
556 
557 //=================================================================================================
558 //
559 // RAND SPECIALIZATION (LONG DOUBLE)
560 //
561 //=================================================================================================
562 
563 //*************************************************************************************************
571 template<>
572 class Rand<long double>
573 {
574  public:
575  //**Generate functions**************************************************************************
578  inline long double generate() const;
579  inline long double generate( long double min, long double max ) const;
581  //**********************************************************************************************
582 
583  //**Randomize functions*************************************************************************
586  inline void randomize( long double& value ) const;
587  inline void randomize( long double& value, long double min, long double max ) const;
589  //**********************************************************************************************
590 };
592 //*************************************************************************************************
593 
594 
595 //*************************************************************************************************
603 inline long double Rand<long double>::generate() const
604 {
605  std::uniform_real_distribution<long double> dist( 0.0, 1.0 );
606  return dist( Random<RNG>::rng_ );
607 }
609 //*************************************************************************************************
610 
611 
612 //*************************************************************************************************
625 inline long double Rand<long double>::generate( long double min, long double max ) const
626 {
627  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
628  std::uniform_real_distribution<long double> dist( min, max );
629  return dist( Random<RNG>::rng_ );
630 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
644 inline void Rand<long double>::randomize( long double& value ) const
645 {
646  value = generate();
647 }
649 //*************************************************************************************************
650 
651 
652 //*************************************************************************************************
666 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
667 {
668  value = generate( min, max );
669 }
671 //*************************************************************************************************
672 
673 
674 
675 
676 //=================================================================================================
677 //
678 // RAND SPECIALIZATION (COMPLEX)
679 //
680 //=================================================================================================
681 
682 //*************************************************************************************************
689 template< typename T > // Type of the values
690 class Rand< complex<T> >
691 {
692  public:
693  //**Generate functions**************************************************************************
696  inline const complex<T> generate() const;
697  inline const complex<T> generate( const T& min, const T& max ) const;
698  inline const complex<T> generate( const T& realmin, const T& realmax,
699  const T& imagmin, const T& imagmax ) const;
701  //**********************************************************************************************
702 
703  //**Randomize functions*************************************************************************
706  inline void randomize( complex<T>& value ) const;
707  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
708  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
709  const T& imagmin, const T& imagmax ) const;
711  //**********************************************************************************************
712 };
714 //*************************************************************************************************
715 
716 
717 //*************************************************************************************************
726 template< typename T > // Type of the values
727 inline const complex<T> Rand< complex<T> >::generate() const
728 {
729  Rand<T> tmp;
730  return complex<T>( tmp.generate(), tmp.generate() );
731 }
733 //*************************************************************************************************
734 
735 
736 //*************************************************************************************************
750 template< typename T > // Type of the values
751 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
752 {
753  Rand<T> tmp;
754  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
755 }
757 //*************************************************************************************************
758 
759 
760 //*************************************************************************************************
777 template< typename T > // Type of the values
778 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
779  const T& imagmin, const T& imagmax ) const
780 {
781  Rand<T> tmp;
782  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
783 }
785 //*************************************************************************************************
786 
787 
788 //*************************************************************************************************
798 template< typename T > // Type of the values
799 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
800 {
801  value = generate();
802 }
804 //*************************************************************************************************
805 
806 
807 //*************************************************************************************************
822 template< typename T > // Type of the values
823 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
824 {
825  value = generate( min, max );
826 }
828 //*************************************************************************************************
829 
830 
831 //*************************************************************************************************
850 template< typename T > // Type of the values
851 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
852  const T& imagmin, const T& imagmax ) const
853 {
854  value = generate( realmin, realmax, imagmin, imagmax );
855 }
857 //*************************************************************************************************
858 
859 
860 
861 
862 //=================================================================================================
863 //
864 // RANDOM NUMBER FUNCTIONS
865 //
866 //=================================================================================================
867 
868 //*************************************************************************************************
882 template< typename T > // Type of the random number
883 inline T rand()
884 {
885  Rand<T> tmp;
886  return tmp.generate();
887 }
888 //*************************************************************************************************
889 
890 
891 //*************************************************************************************************
900 template< typename T // Type of the random number
901  , typename... Args > // Types of the optional arguments
902 inline T rand( Args&&... args )
903 {
904  Rand<T> tmp;
905  return tmp.generate( std::forward<Args>( args )... );
906 }
907 //*************************************************************************************************
908 
909 
910 //*************************************************************************************************
925 template< typename T > // Type of the random number
926 inline void randomize( T& value )
927 {
928  Rand<T> tmp;
929  tmp.randomize( value );
930 }
931 //*************************************************************************************************
932 
933 
934 //*************************************************************************************************
944 template< typename T // Type of the random number
945  , typename... Args > // Types of the optional arguments
946 inline void randomize( T& value, Args&&... args )
947 {
948  Rand<T> tmp;
949  tmp.randomize( value, std::forward<Args>( args )... );
950 }
951 //*************************************************************************************************
952 
953 
954 //*************************************************************************************************
961 {
962  static const uint32_t seed = static_cast<uint32_t>( std::time(0) );
963  return seed;
964 }
965 //*************************************************************************************************
966 
967 
968 //*************************************************************************************************
975 {
976  return Random<RNG>::seed_;
977 }
978 //*************************************************************************************************
979 
980 
981 //*************************************************************************************************
991 inline void setSeed( uint32_t seed )
992 {
993  Random<RNG>::seed_ = seed;
994  Random<RNG>::rng_.seed( seed );
995 }
996 //*************************************************************************************************
997 
998 } // namespace blaze
999 
1000 #endif
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:974
Header file for basic type definitions.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:926
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
32-bit unsigned integer type of the Blaze library.
Header file for the random number generator used in the Blaze library.
uint32_t defaultSeed()
Returns the default random seed.
Definition: Random.h:960
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:289
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:991
T rand()
Random number function.
Definition: Random.h:883
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:218
Random number generator.The Random class encapsulates the initialization of the given random number g...
Definition: Random.h:168
static uint32_t seed_
The current seed for the variate generator.
Definition: Random.h:174
static Type rng_
The mersenne twister variate generator.
Definition: Random.h:175
T generate() const
Generation of a random value in the range .
Definition: Random.h:249
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