int main() { // random number generator std::random_device rd; std::mt19937 urng(rd()); // Create a threadpool with 5 threads ::Uplinkzero::Common::ThreadPool<Uplinkzero::Common::ThreadData> threadPool(5); // Pointer to the function we want the threads in the pool to execute auto pFunctionForThreadToRun = std::function<void(Uplinkzero::Common::FunctionParams &)>(functionForThreadToRun); // enque tasks into the threadpool for (int i = 0; i < 1000; i++) { Uplinkzero::Common::FunctionParams params; params.i = urng(); Uplinkzero::Common::ThreadData td; td.setThreadFunction(pFunctionForThreadToRun); td.setThreadFunctionParams(params); threadPool.enqueue(td); } // sleep for 10 seconds std::this_thread::sleep_for(std::chrono::milliseconds(10000)); std::cout << "Main thread finished!" << std::endl; return 0; }
int operator[](size_t index) const { // Could be more specific exception std::out_of_range if( index >= _size ) { std::ostringstream msg; msg << "Index is out of bounds:\n"; msg << "index = " << index << " not in expected "; msg << "[0, " << _size << ") range\n"; SKYLARK_THROW_EXCEPTION ( base::random123_exception() << base::error_msg(msg.str()) ); } ctr_t ctr; ctr.v[0] = static_cast<ctr_t::value_type>(_base + index); ctr.v[1] = static_cast<ctr_t::value_type>(0); URNG_t urng(ctr, _key); return urng(); }
/** * Shuffles a pile of card objects * @warning Implented with newer c++14 randomization. Requires <random> header */ void CardPile::shuffle() { //c++14 'proper randomization std::random_device rng; std::mt19937 urng(rng()); std::shuffle(_cardPile.begin(), _cardPile.end(), urng); //std::random_shuffle(_cardPile.begin(), _cardPile.end()); //deprecated randomization, required srand }
void Friday::initRobinsonDeck() { //std::random_shuffle (m_startingCards.begin(), m_startingCards.end()); std::vector<RobinsonCard> aux(m_startingCards); std::random_device rng; std::mt19937 urng(rng()); std::shuffle(m_startingCards.begin(), m_startingCards.end(), urng); for (auto row: m_startingCards) { m_robinsonDeck.push(&row); } }
inline std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) { std::vector<TestCase> sorted = unsortedTestCases; switch( config.runOrder() ) { case RunTests::InLexicographicalOrder: std::sort( sorted.begin(), sorted.end(), LexSort() ); break; case RunTests::InRandomOrder: { seedRng( config ); std::random_device rng; std::mt19937 urng(rng()); std::shuffle( sorted.begin(), sorted.end(), rng ); } break; case RunTests::InDeclarationOrder: // already in declaration order break; } return sorted; }
int utils::pick_a_number(int from, int thru) { static std::uniform_int_distribution<> d{}; using parm_t = std::uniform_int_distribution<>::param_type; return d(urng(), parm_t{ from, thru }); }
int utils::coin_toss(double p) { static std::bernoulli_distribution d{}; using parm_t = decltype(d)::param_type; return d(urng(), parm_t{ p }); }
int main() { typedef double real_type; typedef unsigned long uint_type; typedef ::std::size_t size_type; typedef ::dcs::math::la::dense_matrix<real_type> matrix_type; typedef ::dcs::math::la::dense_vector<real_type> vector_type; uint_type seed(5489); uint_type num_obs(50); real_type ff(0.98); // forgetting factor uint_type n_a(2); uint_type n_b(2); uint_type delay(0); ::dcs::math::random::mt19937 urng(seed); ::dcs::math::stats::normal_distribution<real_type> dist; // Create a SISO model with ARX structure vector_type a(n_a); a(0) = -1.5; a(1) = 1.0; vector_type b(n_b+1); b(0) = 0; b(1) = 1.0; b(2) = 0.5; real_type c = 1.0; // noise variance ::dcs::sysid::darx_siso_model<vector_type,real_type,uint_type> siso_model(a, b, c); ::std::cout << siso_model << ::std::endl; // Generate random input data ::dcs::math::la::dense_vector<real_type> u(num_obs); for (size_type i = 0; i < num_obs; ++i) { u(i) = ::dcs::math::sign(::dcs::math::stats::rand(dist, urng)); } // Generate random noise ::dcs::math::la::dense_vector<real_type> e(num_obs); for (size_type i = 0; i < num_obs; ++i) { e(i) = 0.2*::dcs::math::stats::rand(dist, urng); } ::std::cout << "RLS with forgetting factor for SISO models:" << ::std::endl; vector_type theta_hat; matrix_type P; vector_type phi; ::std::cout << "N_A: " << n_a << ::std::endl; ::std::cout << "N_B: " << n_b << ::std::endl; ::std::cout << "D: " << delay << ::std::endl; ::dcs::sysid::rls_arx_siso_init(n_a, n_b, delay, theta_hat, P, phi); ::std::cout << "\tInput Data: " << u << ::std::endl; ::std::cout << "\tNoise Data: " << e << ::std::endl; ::std::cout << "\tInitial Estimated Parameters: " << theta_hat << ::std::endl; ::std::cout << "\tInitial Covariance Matrix: " << P << ::std::endl; ::std::cout << "\tInitial Regressor: " << phi << ::std::endl; vector_type y; y = ::dcs::sysid::simulate(siso_model, u, e); for (size_type i = 0; i < num_obs; ++i) { ::std::cout << "\n\tObservation #" << i << ::std::endl; ::dcs::sysid::rls_ff_arx_siso(y(i), u(i), ff, n_a, n_b, delay, theta_hat, P, phi); //real_type y_hat = ::dcs::math::la::inner_prod(theta_hat, phi); ::std::cout << "\t\tInput Data: " << u(i) << ::std::endl; ::std::cout << "\t\tOutput Data: " << y(i) << ::std::endl; ::std::cout << "\t\tEstimated Parameters: " << theta_hat << ::std::endl; ::std::cout << "\t\tCovariance Matrix: " << P << ::std::endl; ::std::cout << "\t\tRegressor: " << phi << ::std::endl; //::std::cout << "\t\tEstimated Output Data: " << y_hat << ::std::endl; //::std::cout << "\t\tRelative Error: " << ::std::abs((y(i)-y_hat)/y(i)) << ::std::endl; } // ::std::cout << "Simulation without noise: " << ::std::endl; // ::std::cout << "\tInput Data: " << u << ::std::endl; // ::std::cout << "\tOutput Data: " << y << ::std::endl; // // y = ::dcs::sysid::simulate(siso_model, u, e); // ::std::cout << "Simulation with noise: " << ::std::endl; // ::std::cout << "\tInput Data: " << u << ::std::endl; // ::std::cout << "\tNoise Data: " << e << ::std::endl; // ::std::cout << "\tOutput Data: " << y << ::std::endl; }