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  //**Constructors********************************************************************************
184  explicit inline Rand();
185  explicit inline Rand( T min, T max );
187  //**********************************************************************************************
188 
189  //**Conversion operators************************************************************************
192  inline operator T() const;
194  //**********************************************************************************************
195 
196  private:
197  //**Member variables****************************************************************************
200  T value_;
201 
202  //**********************************************************************************************
203 };
204 //*************************************************************************************************
205 
206 
207 //*************************************************************************************************
214 template< typename T > // Type of the random number
216  : value_() // The random number
217 {
218  boost::uniform_int<T> dist( 0, std::numeric_limits<T>::max() );
219  value_ = dist( Random<RNG>::rng_ );
220 }
221 //*************************************************************************************************
222 
223 
224 //*************************************************************************************************
235 template< typename T > // Type of the random number
236 inline Rand<T>::Rand( T min, T max )
237  : value_() // The random number
238 {
239  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
240  boost::uniform_smallint<T> dist( min, max );
241  value_ = dist( Random<RNG>::rng_ );
242 }
243 //*************************************************************************************************
244 
245 
246 //*************************************************************************************************
251 template< typename T > // Type of the random number
252 inline Rand<T>::operator T() const
253 {
254  return value_;
255 }
256 //*************************************************************************************************
257 
258 
259 
260 
261 //=================================================================================================
262 //
263 // RAND SPECIALIZATION (FLOAT)
264 //
265 //=================================================================================================
266 
267 //*************************************************************************************************
275 template<>
276 class Rand<float>
277 {
278  public:
279  //**Constructors********************************************************************************
282  explicit inline Rand();
283  explicit inline Rand( float min, float max );
285  //**********************************************************************************************
286 
287  //**Conversion operators************************************************************************
290  inline operator float() const;
292  //**********************************************************************************************
293 
294  private:
295  //**Member variables****************************************************************************
298  float value_;
299 
300  //**********************************************************************************************
301 };
303 //*************************************************************************************************
304 
305 
306 //*************************************************************************************************
313 inline Rand<float>::Rand()
314  : value_() // The random, single precision value.
315 {
316  boost::uniform_real<float> dist( 0.0, 1.0 );
317  value_ = dist( Random<RNG>::rng_ );
318 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
335 inline Rand<float>::Rand( float min, float max )
336  : value_() // TODO
337 {
338  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
339  boost::uniform_real<float> dist( min, max );
340  value_ = dist( Random<RNG>::rng_ );
341 }
343 //*************************************************************************************************
344 
345 
346 //*************************************************************************************************
352 inline Rand<float>::operator float() const
353 {
354  return value_;
355 }
357 //*************************************************************************************************
358 
359 
360 
361 
362 //=================================================================================================
363 //
364 // RAND SPECIALIZATION (DOUBLE)
365 //
366 //=================================================================================================
367 
368 //*************************************************************************************************
376 template<>
377 class Rand<double>
378 {
379  public:
380  //**Constructors********************************************************************************
383  explicit inline Rand();
384  explicit inline Rand( double min, double max );
386  //**********************************************************************************************
387 
388  //**Conversion operators************************************************************************
391  inline operator double() const;
393  //**********************************************************************************************
394 
395  private:
396  //**Member variables****************************************************************************
399  double value_;
400 
401  //**********************************************************************************************
402 };
404 //*************************************************************************************************
405 
406 
407 //*************************************************************************************************
414 inline Rand<double>::Rand()
415  : value_() // The random, double precision value.
416 {
417  boost::uniform_real<double> dist( 0.0, 1.0 );
418  value_ = dist( Random<RNG>::rng_ );
419 }
421 //*************************************************************************************************
422 
423 
424 //*************************************************************************************************
436 inline Rand<double>::Rand( double min, double max )
437  : value_() // TODO
438 {
439  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
440  boost::uniform_real<double> dist( min, max );
441  value_ = dist( Random<RNG>::rng_ );
442 }
444 //*************************************************************************************************
445 
446 
447 //*************************************************************************************************
453 inline Rand<double>::operator double() const
454 {
455  return value_;
456 }
458 //*************************************************************************************************
459 
460 
461 
462 
463 //=================================================================================================
464 //
465 // RAND SPECIALIZATION (LONG DOUBLE)
466 //
467 //=================================================================================================
468 
469 //*************************************************************************************************
477 template<>
478 class Rand<long double>
479 {
480  public:
481  //**Constructors********************************************************************************
484  explicit inline Rand();
485  explicit inline Rand( long double min, long double max );
487  //**********************************************************************************************
488 
489  //**Conversion operators************************************************************************
492  inline operator long double() const;
494  //**********************************************************************************************
495 
496  private:
497  //**Member variables****************************************************************************
500  long double value_;
501 
502  //**********************************************************************************************
503 };
505 //*************************************************************************************************
506 
507 
508 //*************************************************************************************************
516  : value_() // The random, extended precision value.
517 {
518  boost::uniform_real<long double> dist( 0.0, 1.0 );
519  value_ = dist( Random<RNG>::rng_ );
520 }
522 //*************************************************************************************************
523 
524 
525 //*************************************************************************************************
537 inline Rand<long double>::Rand( long double min, long double max )
538  : value_() // TODO
539 {
540  BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
541  boost::uniform_real<long double> dist( min, max );
542  value_ = dist( Random<RNG>::rng_ );
543 }
545 //*************************************************************************************************
546 
547 
548 //*************************************************************************************************
554 inline Rand<long double>::operator long double() const
555 {
556  return value_;
557 }
559 //*************************************************************************************************
560 
561 
562 
563 
564 //=================================================================================================
565 //
566 // RAND SPECIALIZATION (COMPLEX)
567 //
568 //=================================================================================================
569 
570 //*************************************************************************************************
577 template< typename T > // Type of the values
578 class Rand< complex<T> >
579 {
580  public:
581  //**Constructors********************************************************************************
584  explicit inline Rand();
585  explicit inline Rand( const T& min, const T& max );
586  explicit inline Rand( const T& realmin, const T& realmax, const T& imagmin, const T& imagmax );
588  //**********************************************************************************************
589 
590  //**Conversion operators************************************************************************
593  inline operator complex<T>() const;
595  //**********************************************************************************************
596 
597  private:
598  //**Member variables****************************************************************************
601  complex<T> value_;
602 
603  //**********************************************************************************************
604 };
606 //*************************************************************************************************
607 
608 
609 //*************************************************************************************************
616 template< typename T > // Type of the values
617 inline Rand< complex<T> >::Rand()
618  : value_( Rand<T>(), Rand<T>() ) // The random, complex value.
619 {}
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
637 template< typename T > // Type of the values
638 inline Rand< complex<T> >::Rand( const T& min, const T& max )
639  : value_( Rand<T>( min, max), Rand<T>( min, max ) ) // The random, complex value
640 {}
642 //*************************************************************************************************
643 
644 
645 //*************************************************************************************************
661 template< typename T > // Type of the values
662 inline Rand< complex<T> >::Rand( const T& realmin, const T& realmax, const T& imagmin, const T& imagmax )
663  : value_( Rand<T>( realmin, realmax ), Rand<T>( imagmin, imagmax ) ) // The random, complex value
664 {}
666 //*************************************************************************************************
667 
668 
669 //*************************************************************************************************
675 template< typename T > // Type of the values
676 inline Rand< complex<T> >::operator complex<T>() const
677 {
678  return value_;
679 }
681 //*************************************************************************************************
682 
683 
684 
685 
686 //=================================================================================================
687 //
688 // RANDOM NUMBER FUNCTIONS
689 //
690 //=================================================================================================
691 
692 //*************************************************************************************************
695 template< typename T >
696 inline T rand();
697 
698 template< typename T, typename A1 >
699 inline T rand( const A1& a1 );
700 
701 template< typename T, typename A1, typename A2 >
702 inline T rand( const A1& a1, const A2& a2 );
703 
704 template< typename T, typename A1, typename A2, typename A3 >
705 inline T rand( const A1& a1, const A2& a2, const A3& a3 );
706 
707 template< typename T, typename A1, typename A2, typename A3, typename A4 >
708 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 );
709 
710 template< typename T, typename A1, typename A2, typename A3, typename A4, typename A5 >
711 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 );
712 
713 inline uint32_t getSeed();
714 inline void setSeed( uint32_t seed );
716 //*************************************************************************************************
717 
718 
719 //*************************************************************************************************
733 template< typename T > // Type of the random number
734 inline T rand()
735 {
736  Rand<T> tmp;
737  return tmp;
738 }
739 //*************************************************************************************************
740 
741 
742 //*************************************************************************************************
751 template< typename T // Type of the random number
752  , typename A1 > // Type of the first argument
753 inline T rand( const A1& a1 )
754 {
755  Rand<T> tmp( a1 );
756  return tmp;
757 }
758 //*************************************************************************************************
759 
760 
761 //*************************************************************************************************
771 template< typename T // Type of the random number
772  , typename A1 // Type of the first argument
773  , typename A2 > // Type of the second argument
774 inline T rand( const A1& a1, const A2& a2 )
775 {
776  Rand<T> tmp( a1, a2 );
777  return tmp;
778 }
779 //*************************************************************************************************
780 
781 
782 //*************************************************************************************************
794 template< typename T // Type of the random number
795  , typename A1 // Type of the first argument
796  , typename A2 // Type of the second argument
797  , typename A3 > // Type of the third argument
798 inline T rand( const A1& a1, const A2& a2, const A3& a3 )
799 {
800  Rand<T> tmp( a1, a2, a3 );
801  return tmp;
802 }
803 //*************************************************************************************************
804 
805 
806 //*************************************************************************************************
819 template< typename T // Type of the random number
820  , typename A1 // Type of the first argument
821  , typename A2 // Type of the second argument
822  , typename A3 // Type of the third argument
823  , typename A4 > // Type of the fourth argument
824 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4 )
825 {
826  Rand<T> tmp( a1, a2, a3, a4 );
827  return tmp;
828 }
829 //*************************************************************************************************
830 
831 
832 //*************************************************************************************************
846 template< typename T // Type of the random number
847  , typename A1 // Type of the first argument
848  , typename A2 // Type of the second argument
849  , typename A3 // Type of the third argument
850  , typename A4 // Type of the fourth argument
851  , typename A5 > // Type of the fifth argument
852 inline T rand( const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5 )
853 {
854  Rand<T> tmp( a1, a2, a3, a4, a5 );
855  return tmp;
856 }
857 //*************************************************************************************************
858 
859 
860 //*************************************************************************************************
867 {
868  return Random<RNG>::seed_;
869 }
870 //*************************************************************************************************
871 
872 
873 //*************************************************************************************************
883 inline void setSeed( uint32_t seed )
884 {
885  Random<RNG>::seed_ = seed;
886  Random<RNG>::rng_.seed( seed );
887 }
888 //*************************************************************************************************
889 
890 } // namespace blaze
891 
892 #endif