c++ - C++11 Random between int x, int y, excluding list<int> -
i need way generate random integers between x,y, 1 time random z generated, need next iteration of x,y exclude z (or improve yet, list of ints exclude). homecoming value has honor "std::uniform_int_distribution<>" condition. there obvious way this, hoping fast version.
int generaterandombetweenexcluding(int min, int max, int exclude) // improve list<int> exclude { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(min, max); // how exclude "exclude" or list<int>? int randnum = dis(gen); homecoming randnum; }
#include <algorithm> #include <vector> #include <random> class norepeat { public: void init(int,int); int getone(); bool hasmore(); private: std::vector<int> list; }; void norepeat::init(int min, int max) // min , max inclusive { list.clear(); list.reserve(max-min+1); for( int i=min; i<=max; ++i ) list.push_back(i); std::random_shuffle( list.begin(), list.end() ); // might want supply own random_func, default uses rand() } bool norepeat::hasmore() { homecoming !list.empty(); } int norepeat::getone() { int ret = list.back(); list.pop_back(); homecoming ret; }
c++ c++11 boost random std
No comments:
Post a Comment