See this related question for a more general use of the Boost Random library.
My questions include selecting a random item from std::list , performing some operation that could potentially involve removing an item from the list, and then selecting another random item until some condition is met.
The boost and for loop code look something like this:
// create and insert elements into list std::list<MyClass> myList; //[...] // select uniformly from list indices boost::uniform_int<> indices( 0, myList.size()-1 ); boost::variate_generator< boost::mt19937, boost::uniform_int<> > selectIndex(boost::mt19937(), indices); for( int i = 0; i <= maxOperations; ++i ) { int index = selectIndex(); MyClass & mc = myList.begin() + index; // do operations with mc, potentially removing it from myList //[...] }
My problem is, as soon as operations performed on an element result in the element being deleted, variate_generator can select an invalid index in the list. I don’t think it makes sense to completely recreate variate_generator every time, especially if I sow it with time (0).
source share