如何洗牌std :: vector?
我正在寻找一种通用的,可重用的方式来洗牌C ++中的std::vector
。 这是我目前的做法,但我认为它不是非常高效,因为它需要一个中间数组,并且需要知道项目types(在本例中为DeckCard):
srand(time(NULL)); cards_.clear(); while (temp.size() > 0) { int idx = rand() % temp.size(); DeckCard* card = temp[idx]; cards_.push_back(card); temp.erase(temp.begin() + idx); }
对于C ++ 98,您可以使用:
#include <algorithm> std::random_shuffle(cards_.begin(), cards_.end());
从C ++ 11开始,你应该更喜欢:
#include <algorithm> #include <random> auto rng = std::default_random_engine {}; std::shuffle(std::begin(cards_), std::end(cards_), rng);
在Coliru上的现场示例
如果您打算每次生成不同的排列,请确保在多次调用std::shuffle
时重用同一个rng
实例!
除了@Cicada所说的,你应该先种子,
srand(unsigned(time(NULL))); std::random_shuffle(cards_.begin(), cards_.end());
Per @ FredLarson的评论:
这个版本的random_shuffle()的随机性是实现定义的,所以它可能根本不使用rand()。 那么srand()将不起作用。
所以YMMV。
// shuffle algorithm example #include <iostream> // std::cout #include <algorithm> // std::shuffle #include <array> // std::array #include <random> // std::default_random_engine #include <chrono> // std::chrono::system_clock int main () { std::array<int,5> foo {1,2,3,4,5}; // obtain a time-based seed: unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); shuffle (foo.begin(), foo.end(), std::default_random_engine(seed)); std::cout << "shuffled elements:"; for (int& x: foo) std::cout << ' ' << x; std::cout << '\n'; return 0; }
如果你正在使用boost,你可以使用这个类( debug_mode
设置为false
,如果你想随机化可以预测,执行之间必须设置为true
):
#include <iostream> #include <ctime> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int.hpp> #include <boost/random/uniform_int_distribution.hpp> #include <boost/random/variate_generator.hpp> #include <algorithm> // std::random_shuffle using namespace std; using namespace boost; class Randomizer { private: static const bool debug_mode = false; random::mt19937 rng_; // The private constructor so that the user can not directly instantiate Randomizer() { if(debug_mode==true){ this->rng_ = random::mt19937(); }else{ this->rng_ = random::mt19937(current_time_nanoseconds()); } }; int current_time_nanoseconds(){ struct timespec tm; clock_gettime(CLOCK_REALTIME, &tm); return tm.tv_nsec; } // C++ 03 // ======== // Dont forget to declare these two. You want to make sure they // are unacceptable otherwise you may accidentally get copies of // your singleton appearing. Randomizer(Randomizer const&); // Don't Implement void operator=(Randomizer const&); // Don't implement public: static Randomizer& get_instance(){ // The only instance of the class is created at the first call get_instance () // and will be destroyed only when the program exits static Randomizer instance; return instance; } template<typename RandomAccessIterator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last){ boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_number_shuffler(rng_, boost::uniform_int<>()); std::random_shuffle(first, last, random_number_shuffler); } int rand(unsigned int floor, unsigned int ceil){ random::uniform_int_distribution<> rand_ = random::uniform_int_distribution<> (floor,ceil); return (rand_(rng_)); } };
比你可以用这个代码testing它:
#include "Randomizer.h" #include <iostream> using namespace std; int main (int argc, char* argv[]) { vector<int> v; v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5); v.push_back(6);v.push_back(7);v.push_back(8);v.push_back(9);v.push_back(10); Randomizer::get_instance().random_shuffle(v.begin(), v.end()); for(unsigned int i=0; i<v.size(); i++){ cout << v[i] << ", "; } return 0; }