Blaze 3.9
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>
49#include <blaze/util/EnableIf.h>
51#include <blaze/util/Types.h>
56
57
58namespace blaze {
59
60//=================================================================================================
61//
62// DOXYGEN DOCUMENTATION
63//
64//=================================================================================================
65
66//*************************************************************************************************
119//*************************************************************************************************
120
121
122
123
124//=================================================================================================
125//
126// ::blaze NAMESPACE FORWARD DECLARATIONS
127//
128//=================================================================================================
129
130//*************************************************************************************************
133template< typename T >
134T rand();
135
136template< typename T, typename... Args >
137T rand( Args&&... args );
138
139template< typename T >
140void randomize( T&& value );
141
142template< typename T, typename... Args >
143void randomize( T&& value, Args&&... args );
144
146
147template< typename RNG = DefaultRNG >
149
150template< typename RNG = DefaultRNG >
151void setSeed( uint32_t seed );
153//*************************************************************************************************
154
155
156
157
158//=================================================================================================
159//
160// CLASS DEFINITION
161//
162//=================================================================================================
163
164//*************************************************************************************************
176template< typename RNG = DefaultRNG > // Type of the random number generator
178 : private NonCreatable
179{
180 private:
181 //**Member variables****************************************************************************
184 static uint32_t seed_;
185 static RNG rng_;
187 //**********************************************************************************************
188
189 //**Friend declarations*************************************************************************
191 template< typename, typename > friend class Rand;
192 template< typename > friend uint32_t getSeed();
193 template< typename > friend void setSeed( uint32_t seed );
195 //**********************************************************************************************
196};
197//*************************************************************************************************
198
199
200
201
202//=================================================================================================
203//
204// DEFINITION AND INITIALIZATION OF THE STATIC MEMBER VARIABLES
205//
206//=================================================================================================
207
208template< typename RNG > uint32_t Random<RNG>::seed_( defaultSeed() );
209template< typename RNG > RNG Random<RNG>::rng_ ( defaultSeed() );
210
211
212
213
214//=================================================================================================
215//
216// CLASS RAND
217//
218//=================================================================================================
219
220//*************************************************************************************************
227template< typename T // Type of the random number
228 , typename = void > // Restricting condition
229class Rand
230{};
231//*************************************************************************************************
232
233
234
235
236//=================================================================================================
237//
238// RAND SPECIALIZATION (INTEGRAL TYPES)
239//
240//=================================================================================================
241
242//*************************************************************************************************
250template< typename T > // Type of the random number
251class Rand< T, EnableIf_t< IsIntegral_v<T> > >
252{
253 public:
254 //**********************************************************************************************
262 inline T generate() const
263 {
264 std::uniform_int_distribution<T> dist( 0, std::numeric_limits<T>::max() );
265 return dist( Random<>::rng_ );
266 }
267 //**********************************************************************************************
268
269 //**********************************************************************************************
281 inline T generate( T min, T max ) const
282 {
283 BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max value pair" );
284 std::uniform_int_distribution<T> dist( min, max );
285 return dist( Random<>::rng_ );
286 }
287 //**********************************************************************************************
288
289 //**********************************************************************************************
298 inline void randomize( T& value ) const
299 {
300 value = generate();
301 }
302 //**********************************************************************************************
303
304 //**********************************************************************************************
317 inline void randomize( T& value, T min, T max ) const
318 {
319 value = generate( min, max );
320 }
321 //**********************************************************************************************
322};
324//*************************************************************************************************
325
326
327
328
329//=================================================================================================
330//
331// RAND SPECIALIZATION (FLOATING POINT TYPES)
332//
333//=================================================================================================
334
335//*************************************************************************************************
343template< typename T > // Type of the random number
344class Rand< T, EnableIf_t< IsFloatingPoint_v<T> > >
345{
346 public:
347 //**********************************************************************************************
354 inline T generate() const
355 {
356 std::uniform_real_distribution<T> dist( 0.0, 1.0 );
357 return dist( Random<>::rng_ );
358 }
359 //**********************************************************************************************
360
361 //**********************************************************************************************
373 inline T generate( T min, T max ) const
374 {
375 BLAZE_INTERNAL_ASSERT( min <= max, "Invalid min/max values" );
376 std::uniform_real_distribution<T> dist( min, max );
377 return dist( Random<>::rng_ );
378 }
379 //*************************************************************************************************
380
381 //*************************************************************************************************
389 inline void randomize( T& value ) const
390 {
391 value = generate();
392 }
393 //*************************************************************************************************
394
395 //*************************************************************************************************
408 inline void randomize( T& value, T min, T max ) const
409 {
410 value = generate( min, max );
411 }
412 //*************************************************************************************************
413};
415//*************************************************************************************************
416
417
418
419
420//=================================================================================================
421//
422// RAND SPECIALIZATION (COMPLEX)
423//
424//=================================================================================================
425
426//*************************************************************************************************
433template< typename T > // Type of the values
434class Rand< complex<T> >
435{
436 public:
437 //**********************************************************************************************
445 inline const complex<T> generate() const
446 {
447 Rand<T> tmp;
448 return complex<T>( tmp.generate(), tmp.generate() );
449 }
450 //**********************************************************************************************
451
452 //**********************************************************************************************
465 inline const complex<T> generate( const T& min, const T& max ) const
466 {
467 Rand<T> tmp;
468 return complex<T>( tmp.generate( min, max ), tmp.generate( min, max ) );
469 }
470 //*************************************************************************************************
471
472 //*************************************************************************************************
488 inline const complex<T> generate( const T& realmin, const T& realmax,
489 const T& imagmin, const T& imagmax ) const
490 {
491 Rand<T> tmp;
492 return complex<T>( tmp.generate( realmin, realmax ), tmp.generate( imagmin, imagmax ) );
493 }
494 //*************************************************************************************************
495
496 //*************************************************************************************************
505 inline void randomize( complex<T>& value ) const
506 {
507 value = generate();
508 }
509 //*************************************************************************************************
510
511 //*************************************************************************************************
525 inline void randomize( complex<T>& value, const T& min, const T& max ) const
526 {
527 value = generate( min, max );
528 }
529 //*************************************************************************************************
530
531 //*************************************************************************************************
549 inline void randomize( complex<T>& value, const T& realmin, const T& realmax,
550 const T& imagmin, const T& imagmax ) const
551 {
552 value = generate( realmin, realmax, imagmin, imagmax );
553 }
554 //*************************************************************************************************
555};
557//*************************************************************************************************
558
559
560
561
562//=================================================================================================
563//
564// RANDOM NUMBER FUNCTIONS
565//
566//=================================================================================================
567
568//*************************************************************************************************
582template< typename T > // Type of the random number
583inline T rand()
584{
586 return tmp.generate();
587}
588//*************************************************************************************************
589
590
591//*************************************************************************************************
600template< typename T // Type of the random number
601 , typename... Args > // Types of the optional arguments
602inline T rand( Args&&... args )
603{
605 return tmp.generate( std::forward<Args>( args )... );
606}
607//*************************************************************************************************
608
609
610//*************************************************************************************************
625template< typename T > // Type of the random number
626inline void randomize( T&& value )
627{
629 tmp.randomize( std::forward<T>( value ) );
630}
631//*************************************************************************************************
632
633
634//*************************************************************************************************
644template< typename T // Type of the random number
645 , typename... Args > // Types of the optional arguments
646inline void randomize( T&& value, Args&&... args )
647{
649 tmp.randomize( std::forward<T>( value ), std::forward<Args>( args )... );
650}
651//*************************************************************************************************
652
653
654//*************************************************************************************************
660inline uint32_t defaultSeed()
661{
662 static const uint32_t seed = static_cast<uint32_t>( std::time(0) );
663 return seed;
664}
665//*************************************************************************************************
666
667
668//*************************************************************************************************
674template< typename RNG > // Type of the random number generator
675inline uint32_t getSeed()
676{
677 return Random<RNG>::seed_;
678}
679//*************************************************************************************************
680
681
682//*************************************************************************************************
692template< typename RNG > // Type of the random number generator
693inline void setSeed( uint32_t seed )
694{
695 Random<RNG>::seed_ = seed;
696 Random<RNG>::rng_.seed( seed );
697}
698//*************************************************************************************************
699
700} // namespace blaze
701
702#endif
Header file for run time assertion macros.
Header file for the complex data type.
Header file for the EnableIf class template.
Header file for the IsFloatingPoint type trait.
Header file for the IsIntegral type trait.
Base class for non-creatable (static) classes.
Header file for the RemoveCVRef type trait.
Header file for the RemoveCV type trait.
Base class for non-creatable (static) classes.
Definition: NonCreatable.h:66
Default implementation of the Rand class.
Definition: Random.h:230
Random number generator.
Definition: Random.h:179
static uint32_t seed_
The current seed for the variate generator.
Definition: Random.h:184
static RNG rng_
The mersenne twister variate generator.
Definition: Random.h:185
Complex data type of the Blaze library.
32-bit unsigned integer type of the Blaze library.
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:1339
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:1375
decltype(auto) generate(size_t m, size_t n, OP op)
Generates a new dense matrix filled via the given custom binary operation.
Definition: DMatGenExpr.h:675
void randomize(T &&value, Args &&... args)
Randomization of a given variable.
Definition: Random.h:646
uint32_t defaultSeed()
Returns the default random seed.
Definition: Random.h:660
uint32_t getSeed()
Returns the current seed of the random number generator.
Definition: Random.h:675
T rand(Args &&... args)
Random number function.
Definition: Random.h:602
void setSeed(uint32_t seed)
Setting the seed of the random number generator.
Definition: Random.h:693
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
constexpr bool IsFloatingPoint_v
Auxiliary variable template for the IsFloatingPoint type trait.
Definition: IsFloatingPoint.h:95
constexpr bool IsIntegral_v
Auxiliary variable template for the IsIntegral type trait.
Definition: IsIntegral.h:95
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.
Definition: EnableIf.h:138
Header file for the random number generator used in the Blaze library.
Header file for basic type definitions.