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
169  : private NonCreatable
170 {
171  private:
172  //**Member variables****************************************************************************
175  static uint32_t seed_;
176  static Type rng_;
177 
178  //**********************************************************************************************
179 
180  //**Friend declarations*************************************************************************
182  template< typename T > friend class Rand;
183  friend uint32_t getSeed();
184  friend void setSeed( uint32_t seed );
186  //**********************************************************************************************
187 };
188 //*************************************************************************************************
189 
190 
191 
192 
193 //=================================================================================================
194 //
195 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
196 //
197 //=================================================================================================
198 
199 template< typename Type > uint32_t Random<Type>::seed_( defaultSeed() );
200 template< typename Type > Type Random<Type>::rng_ ( defaultSeed() );
201 
202 
203 
204 
205 //=================================================================================================
206 //
207 // CLASS RAND
208 //
209 //=================================================================================================
210 
211 //*************************************************************************************************
218 template< typename T > // Type of the random number
219 class Rand
220 {
221  public:
222  //**Generate functions**************************************************************************
225  inline T generate() const;
226  inline T generate( T min, T max ) const;
228  //**********************************************************************************************
229 
230  //**Randomize functions*************************************************************************
233  inline void randomize( T& value ) const;
234  inline void randomize( T& value, T min, T max ) const;
236  //**********************************************************************************************
237 };
238 //*************************************************************************************************
239 
240 
241 //*************************************************************************************************
249 template< typename T > // Type of the random number
250 inline T Rand<T>::generate() const
251 {
252  std::uniform_int_distribution<T> dist( 0, std::numeric_limits<T>::max() );
253  return dist( Random<RNG>::rng_ );
254 }
255 //*************************************************************************************************
256 
257 
258 //*************************************************************************************************
270 template< typename T > // Type of the random number
271 inline T Rand<T>::generate( T min, T max ) const
272 {
273  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
274  std::uniform_int_distribution<T> dist( min, max );
275  return dist( Random<RNG>::rng_ );
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
289 template< typename T > // Type of the random number
290 inline void Rand<T>::randomize( T& value ) const
291 {
292  value = generate();
293 }
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
310 template< typename T > // Type of the random number
311 inline void Rand<T>::randomize( T& value, T min, T max ) const
312 {
313  value = generate( min, max );
314 }
315 //*************************************************************************************************
316 
317 
318 
319 
320 //=================================================================================================
321 //
322 // RAND SPECIALIZATION (FLOAT)
323 //
324 //=================================================================================================
325 
326 //*************************************************************************************************
334 template<>
335 class Rand<float>
336 {
337  public:
338  //**Generate functions**************************************************************************
341  inline float generate() const;
342  inline float generate( float min, float max ) const;
344  //**********************************************************************************************
345 
346  //**Randomize functions*************************************************************************
349  inline void randomize( float& value ) const;
350  inline void randomize( float& value, float min, float max ) const;
352  //**********************************************************************************************
353 };
355 //*************************************************************************************************
356 
357 
358 //*************************************************************************************************
366 inline float Rand<float>::generate() const
367 {
368  std::uniform_real_distribution<float> dist( 0.0, 1.0 );
369  return dist( Random<RNG>::rng_ );
370 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
388 inline float Rand<float>::generate( float min, float max ) const
389 {
390  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
391  std::uniform_real_distribution<float> dist( min, max );
392  return dist( Random<RNG>::rng_ );
393 }
395 //*************************************************************************************************
396 
397 
398 //*************************************************************************************************
407 inline void Rand<float>::randomize( float& value ) const
408 {
409  value = generate();
410 }
412 //*************************************************************************************************
413 
414 
415 //*************************************************************************************************
429 inline void Rand<float>::randomize( float& value, float min, float max ) const
430 {
431  value = generate( min, max );
432 }
434 //*************************************************************************************************
435 
436 
437 
438 
439 //=================================================================================================
440 //
441 // RAND SPECIALIZATION (DOUBLE)
442 //
443 //=================================================================================================
444 
445 //*************************************************************************************************
453 template<>
454 class Rand<double>
455 {
456  public:
457  //**Generate functions**************************************************************************
460  inline double generate() const;
461  inline double generate( double min, double max ) const;
463  //**********************************************************************************************
464 
465  //**Randomize functions*************************************************************************
468  inline void randomize( double& value ) const;
469  inline void randomize( double& value, double min, double max ) const;
471  //**********************************************************************************************
472 };
474 //*************************************************************************************************
475 
476 
477 //*************************************************************************************************
485 inline double Rand<double>::generate() const
486 {
487  std::uniform_real_distribution<double> dist( 0.0, 1.0 );
488  return dist( Random<RNG>::rng_ );
489 }
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
507 inline double Rand<double>::generate( double min, double max ) const
508 {
509  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
510  std::uniform_real_distribution<double> dist( min, max );
511  return dist( Random<RNG>::rng_ );
512 }
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
526 inline void Rand<double>::randomize( double& value ) const
527 {
528  value = generate();
529 }
531 //*************************************************************************************************
532 
533 
534 //*************************************************************************************************
548 inline void Rand<double>::randomize( double& value, double min, double max ) const
549 {
550  value = generate( min, max );
551 }
553 //*************************************************************************************************
554 
555 
556 
557 
558 //=================================================================================================
559 //
560 // RAND SPECIALIZATION (LONG DOUBLE)
561 //
562 //=================================================================================================
563 
564 //*************************************************************************************************
572 template<>
573 class Rand<long double>
574 {
575  public:
576  //**Generate functions**************************************************************************
579  inline long double generate() const;
580  inline long double generate( long double min, long double max ) const;
582  //**********************************************************************************************
583 
584  //**Randomize functions*************************************************************************
587  inline void randomize( long double& value ) const;
588  inline void randomize( long double& value, long double min, long double max ) const;
590  //**********************************************************************************************
591 };
593 //*************************************************************************************************
594 
595 
596 //*************************************************************************************************
604 inline long double Rand<long double>::generate() const
605 {
606  std::uniform_real_distribution<long double> dist( 0.0, 1.0 );
607  return dist( Random<RNG>::rng_ );
608 }
610 //*************************************************************************************************
611 
612 
613 //*************************************************************************************************
626 inline long double Rand<long double>::generate( long double min, long double max ) const
627 {
628  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
629  std::uniform_real_distribution<long double> dist( min, max );
630  return dist( Random<RNG>::rng_ );
631 }
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
645 inline void Rand<long double>::randomize( long double& value ) const
646 {
647  value = generate();
648 }
650 //*************************************************************************************************
651 
652 
653 //*************************************************************************************************
667 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
668 {
669  value = generate( min, max );
670 }
672 //*************************************************************************************************
673 
674 
675 
676 
677 //=================================================================================================
678 //
679 // RAND SPECIALIZATION (COMPLEX)
680 //
681 //=================================================================================================
682 
683 //*************************************************************************************************
690 template< typename T > // Type of the values
691 class Rand< complex<T> >
692 {
693  public:
694  //**Generate functions**************************************************************************
697  inline const complex<T> generate() const;
698  inline const complex<T> generate( const T& min, const T& max ) const;
699  inline const complex<T> generate( const T& realmin, const T& realmax,
700  const T& imagmin, const T& imagmax ) const;
702  //**********************************************************************************************
703 
704  //**Randomize functions*************************************************************************
707  inline void randomize( complex<T>& value ) const;
708  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
709  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
710  const T& imagmin, const T& imagmax ) const;
712  //**********************************************************************************************
713 };
715 //*************************************************************************************************
716 
717 
718 //*************************************************************************************************
727 template< typename T > // Type of the values
728 inline const complex<T> Rand< complex<T> >::generate() const
729 {
730  Rand<T> tmp;
731  return complex<T>( tmp.generate(), tmp.generate() );
732 }
734 //*************************************************************************************************
735 
736 
737 //*************************************************************************************************
751 template< typename T > // Type of the values
752 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
753 {
754  Rand<T> tmp;
755  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
756 }
758 //*************************************************************************************************
759 
760 
761 //*************************************************************************************************
778 template< typename T > // Type of the values
779 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
780  const T& imagmin, const T& imagmax ) const
781 {
782  Rand<T> tmp;
783  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
784 }
786 //*************************************************************************************************
787 
788 
789 //*************************************************************************************************
799 template< typename T > // Type of the values
800 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
801 {
802  value = generate();
803 }
805 //*************************************************************************************************
806 
807 
808 //*************************************************************************************************
823 template< typename T > // Type of the values
824 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
825 {
826  value = generate( min, max );
827 }
829 //*************************************************************************************************
830 
831 
832 //*************************************************************************************************
851 template< typename T > // Type of the values
852 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
853  const T& imagmin, const T& imagmax ) const
854 {
855  value = generate( realmin, realmax, imagmin, imagmax );
856 }
858 //*************************************************************************************************
859 
860 
861 
862 
863 //=================================================================================================
864 //
865 // RANDOM NUMBER FUNCTIONS
866 //
867 //=================================================================================================
868 
869 //*************************************************************************************************
883 template< typename T > // Type of the random number
884 inline T rand()
885 {
886  Rand<T> tmp;
887  return tmp.generate();
888 }
889 //*************************************************************************************************
890 
891 
892 //*************************************************************************************************
901 template< typename T // Type of the random number
902  , typename... Args > // Types of the optional arguments
903 inline T rand( Args&&... args )
904 {
905  Rand<T> tmp;
906  return tmp.generate( std::forward<Args>( args )... );
907 }
908 //*************************************************************************************************
909 
910 
911 //*************************************************************************************************
926 template< typename T > // Type of the random number
927 inline void randomize( T& value )
928 {
929  Rand<T> tmp;
930  tmp.randomize( value );
931 }
932 //*************************************************************************************************
933 
934 
935 //*************************************************************************************************
945 template< typename T // Type of the random number
946  , typename... Args > // Types of the optional arguments
947 inline void randomize( T& value, Args&&... args )
948 {
949  Rand<T> tmp;
950  tmp.randomize( value, std::forward<Args>( args )... );
951 }
952 //*************************************************************************************************
953 
954 
955 //*************************************************************************************************
962 {
963  static const uint32_t seed = static_cast<uint32_t>( std::time(0) );
964  return seed;
965 }
966 //*************************************************************************************************
967 
968 
969 //*************************************************************************************************
976 {
977  return Random<RNG>::seed_;
978 }
979 //*************************************************************************************************
980 
981 
982 //*************************************************************************************************
992 inline void setSeed( uint32_t seed )
993 {
994  Random<RNG>::seed_ = seed;
995  Random<RNG>::rng_.seed( seed );
996 }
997 //*************************************************************************************************
998 
999 } // namespace blaze
1000 
1001 #endif
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:975
Header file for basic type definitions.
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:927
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
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:961
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:290
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:992
T rand()
Random number function.
Definition: Random.h:884
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:219
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:175
static Type rng_
The mersenne twister variate generator.
Definition: Random.h:176
T generate() const
Generation of a random value in the range .
Definition: Random.h:250
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