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>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // DOXYGEN DOCUMENTATION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
116 //*************************************************************************************************
117 
118 
119 
120 
121 //=================================================================================================
122 //
123 // ::blaze NAMESPACE FORWARD DECLARATIONS
124 //
125 //=================================================================================================
126 
127 //*************************************************************************************************
130 template< typename T >
131 inline T rand();
132 
133 template< typename T, typename... Args >
134 inline T rand( Args&&... args );
135 
136 template< typename T >
137 inline void randomize( T&& value );
138 
139 template< typename T, typename... Args >
140 inline void randomize( T&& value, Args&&... args );
141 
142 inline uint32_t defaultSeed();
143 inline uint32_t getSeed();
144 inline void setSeed( uint32_t seed );
146 //*************************************************************************************************
147 
148 
149 
150 
151 //=================================================================================================
152 //
153 // CLASS DEFINITION
154 //
155 //=================================================================================================
156 
157 //*************************************************************************************************
169 template< typename Type > // Type of the random number generator
170 class Random
171  : private NonCreatable
172 {
173  private:
174  //**Member variables****************************************************************************
177  static uint32_t seed_;
178  static Type rng_;
179 
180  //**********************************************************************************************
181 
182  //**Friend declarations*************************************************************************
184  template< typename T > friend class Rand;
185  friend uint32_t getSeed();
186  friend void setSeed( uint32_t seed );
188  //**********************************************************************************************
189 };
190 //*************************************************************************************************
191 
192 
193 
194 
195 //=================================================================================================
196 //
197 // DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
198 //
199 //=================================================================================================
200 
201 template< typename Type > uint32_t Random<Type>::seed_( defaultSeed() );
202 template< typename Type > Type Random<Type>::rng_ ( defaultSeed() );
203 
204 
205 
206 
207 //=================================================================================================
208 //
209 // CLASS RAND
210 //
211 //=================================================================================================
212 
213 //*************************************************************************************************
220 template< typename T > // Type of the random number
221 class Rand
222 {
223  public:
224  //**Generate functions**************************************************************************
227  inline T generate() const;
228  inline T generate( T min, T max ) const;
230  //**********************************************************************************************
231 
232  //**Randomize functions*************************************************************************
235  inline void randomize( T& value ) const;
236  inline void randomize( T& value, T min, T max ) const;
238  //**********************************************************************************************
239 };
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
251 template< typename T > // Type of the random number
252 inline T Rand<T>::generate() const
253 {
254  std::uniform_int_distribution<T> dist( 0, std::numeric_limits<T>::max() );
255  return dist( Random<RNG>::rng_ );
256 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
272 template< typename T > // Type of the random number
273 inline T Rand<T>::generate( T min, T max ) const
274 {
275  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
276  std::uniform_int_distribution<T> dist( min, max );
277  return dist( Random<RNG>::rng_ );
278 }
279 //*************************************************************************************************
280 
281 
282 //*************************************************************************************************
291 template< typename T > // Type of the random number
292 inline void Rand<T>::randomize( T& value ) const
293 {
294  value = generate();
295 }
296 //*************************************************************************************************
297 
298 
299 //*************************************************************************************************
312 template< typename T > // Type of the random number
313 inline void Rand<T>::randomize( T& value, T min, T max ) const
314 {
315  value = generate( min, max );
316 }
317 //*************************************************************************************************
318 
319 
320 
321 
322 //=================================================================================================
323 //
324 // RAND SPECIALIZATION (FLOAT)
325 //
326 //=================================================================================================
327 
328 //*************************************************************************************************
336 template<>
337 class Rand<float>
338 {
339  public:
340  //**Generate functions**************************************************************************
343  inline float generate() const;
344  inline float generate( float min, float max ) const;
346  //**********************************************************************************************
347 
348  //**Randomize functions*************************************************************************
351  inline void randomize( float& value ) const;
352  inline void randomize( float& value, float min, float max ) const;
354  //**********************************************************************************************
355 };
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
368 inline float Rand<float>::generate() const
369 {
370  std::uniform_real_distribution<float> dist( 0.0, 1.0 );
371  return dist( Random<RNG>::rng_ );
372 }
374 //*************************************************************************************************
375 
376 
377 //*************************************************************************************************
390 inline float Rand<float>::generate( float min, float max ) const
391 {
392  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
393  std::uniform_real_distribution<float> dist( min, max );
394  return dist( Random<RNG>::rng_ );
395 }
397 //*************************************************************************************************
398 
399 
400 //*************************************************************************************************
409 inline void Rand<float>::randomize( float& value ) const
410 {
411  value = generate();
412 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
431 inline void Rand<float>::randomize( float& value, float min, float max ) const
432 {
433  value = generate( min, max );
434 }
436 //*************************************************************************************************
437 
438 
439 
440 
441 //=================================================================================================
442 //
443 // RAND SPECIALIZATION (DOUBLE)
444 //
445 //=================================================================================================
446 
447 //*************************************************************************************************
455 template<>
456 class Rand<double>
457 {
458  public:
459  //**Generate functions**************************************************************************
462  inline double generate() const;
463  inline double generate( double min, double max ) const;
465  //**********************************************************************************************
466 
467  //**Randomize functions*************************************************************************
470  inline void randomize( double& value ) const;
471  inline void randomize( double& value, double min, double max ) const;
473  //**********************************************************************************************
474 };
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
487 inline double Rand<double>::generate() const
488 {
489  std::uniform_real_distribution<double> dist( 0.0, 1.0 );
490  return dist( Random<RNG>::rng_ );
491 }
493 //*************************************************************************************************
494 
495 
496 //*************************************************************************************************
509 inline double Rand<double>::generate( double min, double max ) const
510 {
511  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
512  std::uniform_real_distribution<double> dist( min, max );
513  return dist( Random<RNG>::rng_ );
514 }
516 //*************************************************************************************************
517 
518 
519 //*************************************************************************************************
528 inline void Rand<double>::randomize( double& value ) const
529 {
530  value = generate();
531 }
533 //*************************************************************************************************
534 
535 
536 //*************************************************************************************************
550 inline void Rand<double>::randomize( double& value, double min, double max ) const
551 {
552  value = generate( min, max );
553 }
555 //*************************************************************************************************
556 
557 
558 
559 
560 //=================================================================================================
561 //
562 // RAND SPECIALIZATION (LONG DOUBLE)
563 //
564 //=================================================================================================
565 
566 //*************************************************************************************************
574 template<>
575 class Rand<long double>
576 {
577  public:
578  //**Generate functions**************************************************************************
581  inline long double generate() const;
582  inline long double generate( long double min, long double max ) const;
584  //**********************************************************************************************
585 
586  //**Randomize functions*************************************************************************
589  inline void randomize( long double& value ) const;
590  inline void randomize( long double& value, long double min, long double max ) const;
592  //**********************************************************************************************
593 };
595 //*************************************************************************************************
596 
597 
598 //*************************************************************************************************
606 inline long double Rand<long double>::generate() const
607 {
608  std::uniform_real_distribution<long double> dist( 0.0, 1.0 );
609  return dist( Random<RNG>::rng_ );
610 }
612 //*************************************************************************************************
613 
614 
615 //*************************************************************************************************
628 inline long double Rand<long double>::generate( long double min, long double max ) const
629 {
630  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
631  std::uniform_real_distribution<long double> dist( min, max );
632  return dist( Random<RNG>::rng_ );
633 }
635 //*************************************************************************************************
636 
637 
638 //*************************************************************************************************
647 inline void Rand<long double>::randomize( long double& value ) const
648 {
649  value = generate();
650 }
652 //*************************************************************************************************
653 
654 
655 //*************************************************************************************************
669 inline void Rand<long double>::randomize( long double& value, long double min, long double max ) const
670 {
671  value = generate( min, max );
672 }
674 //*************************************************************************************************
675 
676 
677 
678 
679 //=================================================================================================
680 //
681 // RAND SPECIALIZATION (COMPLEX)
682 //
683 //=================================================================================================
684 
685 //*************************************************************************************************
692 template< typename T > // Type of the values
693 class Rand< complex<T> >
694 {
695  public:
696  //**Generate functions**************************************************************************
699  inline const complex<T> generate() const;
700  inline const complex<T> generate( const T& min, const T& max ) const;
701  inline const complex<T> generate( const T& realmin, const T& realmax,
702  const T& imagmin, const T& imagmax ) const;
704  //**********************************************************************************************
705 
706  //**Randomize functions*************************************************************************
709  inline void randomize( complex<T>& value ) const;
710  inline void randomize( complex<T>& value, const T& min, const T& max ) const;
711  inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
712  const T& imagmin, const T& imagmax ) const;
714  //**********************************************************************************************
715 };
717 //*************************************************************************************************
718 
719 
720 //*************************************************************************************************
729 template< typename T > // Type of the values
730 inline const complex<T> Rand< complex<T> >::generate() const
731 {
732  Rand<T> tmp;
733  return complex<T>( tmp.generate(), tmp.generate() );
734 }
736 //*************************************************************************************************
737 
738 
739 //*************************************************************************************************
753 template< typename T > // Type of the values
754 inline const complex<T> Rand< complex<T> >::generate( const T& min, const T& max ) const
755 {
756  Rand<T> tmp;
757  return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
758 }
760 //*************************************************************************************************
761 
762 
763 //*************************************************************************************************
780 template< typename T > // Type of the values
781 inline const complex<T> Rand< complex<T> >::generate( const T& realmin, const T& realmax,
782  const T& imagmin, const T& imagmax ) const
783 {
784  Rand<T> tmp;
785  return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
786 }
788 //*************************************************************************************************
789 
790 
791 //*************************************************************************************************
801 template< typename T > // Type of the values
802 inline void Rand< complex<T> >::randomize( complex<T>& value ) const
803 {
804  value = generate();
805 }
807 //*************************************************************************************************
808 
809 
810 //*************************************************************************************************
825 template< typename T > // Type of the values
826 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& min, const T& max ) const
827 {
828  value = generate( min, max );
829 }
831 //*************************************************************************************************
832 
833 
834 //*************************************************************************************************
853 template< typename T > // Type of the values
854 inline void Rand< complex<T> >::randomize( complex<T>& value, const T& realmin, const T& realmax,
855  const T& imagmin, const T& imagmax ) const
856 {
857  value = generate( realmin, realmax, imagmin, imagmax );
858 }
860 //*************************************************************************************************
861 
862 
863 
864 
865 //=================================================================================================
866 //
867 // RANDOM NUMBER FUNCTIONS
868 //
869 //=================================================================================================
870 
871 //*************************************************************************************************
885 template< typename T > // Type of the random number
886 inline T rand()
887 {
888  Rand< RemoveCV_t<T> > tmp;
889  return tmp.generate();
890 }
891 //*************************************************************************************************
892 
893 
894 //*************************************************************************************************
903 template< typename T // Type of the random number
904  , typename... Args > // Types of the optional arguments
905 inline T rand( Args&&... args )
906 {
907  Rand< RemoveCV_t<T> > tmp;
908  return tmp.generate( std::forward<Args>( args )... );
909 }
910 //*************************************************************************************************
911 
912 
913 //*************************************************************************************************
928 template< typename T > // Type of the random number
929 inline void randomize( T&& value )
930 {
931  Rand< Decay_t<T> > tmp;
932  tmp.randomize( std::forward<T>( value ) );
933 }
934 //*************************************************************************************************
935 
936 
937 //*************************************************************************************************
947 template< typename T // Type of the random number
948  , typename... Args > // Types of the optional arguments
949 inline void randomize( T&& value, Args&&... args )
950 {
951  Rand< Decay_t<T> > tmp;
952  tmp.randomize( std::forward<T>( value ), std::forward<Args>( args )... );
953 }
954 //*************************************************************************************************
955 
956 
957 //*************************************************************************************************
964 {
965  static const uint32_t seed = static_cast<uint32_t>( std::time(0) );
966  return seed;
967 }
968 //*************************************************************************************************
969 
970 
971 //*************************************************************************************************
978 {
979  return Random<RNG>::seed_;
980 }
981 //*************************************************************************************************
982 
983 
984 //*************************************************************************************************
994 inline void setSeed( uint32_t seed )
995 {
996  Random<RNG>::seed_ = seed;
997  Random<RNG>::rng_.seed( seed );
998 }
999 //*************************************************************************************************
1000 
1001 } // namespace blaze
1002 
1003 #endif
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:977
Header file for basic type definitions.
Header file for the RemoveCV type trait.
void randomize(T &&value)
Randomization of a given variable.
Definition: Random.h:929
32-bit unsigned integer type of the Blaze library.
Header file for the random number generator used in the Blaze library.
Header file for the Decay type trait.
uint32_t defaultSeed()
Returns the default random seed.
Definition: Random.h:963
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:292
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:994
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
T rand()
Random number function.
Definition: Random.h:886
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:221
Random number generator.The Random class encapsulates the initialization of the given random number g...
Definition: Random.h:170
static uint32_t seed_
The current seed for the variate generator.
Definition: Random.h:177
static Type rng_
The mersenne twister variate generator.
Definition: Random.h:178
T generate() const
Generation of a random value in the range .
Definition: Random.h:252
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