Skip to content
Snippets Groups Projects
Commit e2440bb2 authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

aux: add random number generator to CLI

I believe we need to push a random number generator algorithm to CLI in
order to write random tests using aql scripts.
parent d862148d
No related branches found
No related tags found
1 merge request!229Merge jt
#include "Random.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto RandomInt = registration::AbstractRegister < debug::Random < int >, int > ( debug::Random < int >::random );
auto RandomUnsigned = registration::AbstractRegister < debug::Random < unsigned >, unsigned > ( debug::Random < unsigned >::random );
} /* namespace */
#pragma once
#include <ext/random>
namespace debug {
template < class T >
class Random {
public:
static T random ( );
};
template < class T >
T Random < T >::random ( ) {
return ext::random_devices::semirandom ( );
}
} /* namespace debug */
#include "Random.h"
#include <registration/AlgoRegistration.hpp>
namespace {
auto RandomInt = registration::AbstractRegister < generate::RandomFactory < int >, int > ( generate::RandomFactory < int >::randomNumber );
auto RandomIntRange = registration::AbstractRegister < generate::RandomFactory < int >, int, const int&, const int& > ( generate::RandomFactory < int >::randomNumber );
auto RandomUnsigned = registration::AbstractRegister < generate::RandomFactory < unsigned >, unsigned > ( generate::RandomFactory < unsigned >::randomNumber );
auto RandomUnsignedRange = registration::AbstractRegister < generate::RandomFactory < unsigned >, unsigned, const unsigned&, const unsigned& > ( generate::RandomFactory < unsigned >::randomNumber );
} /* namespace */
#pragma once
#include <ext/random>
namespace generate {
template < class T >
class RandomFactory {
public:
static T randomNumber ( );
/** @brief Returns random number within range lo (inclusive) and hi (exclusive) */
static T randomNumber ( const T& lo, const T& hi );
};
template < class T >
T RandomFactory < T >::randomNumber ( ) {
return ext::random_devices::semirandom ( );
}
template < class T >
T RandomFactory < T >::randomNumber ( const T& lo, const T& hi ) {
return ext::random_devices::semirandom ( lo, hi );
}
} /* namespace generate */
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
   
#include <random> #include <random>
#include <limits> #include <limits>
#include <stdexcept>
   
namespace ext { namespace ext {
   
...@@ -92,6 +93,17 @@ private: ...@@ -92,6 +93,17 @@ private:
return dis(gen); return dis(gen);
} }
   
/** \brief Returns random value withing lo (inclusive) and hi (exclusive)
* \return semirandom value
*/
result_type operator()( const result_type& lo, const result_type& hi ) {
if ( lo >= hi ) {
throw std::invalid_argument ( "Empty range." );
}
return ( dis(gen) % ( hi - lo ) ) + lo;
}
/** /**
* \brief * \brief
* Getter of the minimal value the semirandom device can produce. * Getter of the minimal value the semirandom device can produce.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment