Suppose that you want to generate N random numbers but that you
want a determined number of replications for each of them: for example
you want 10 times the number 1, 25 times the number 2 and 15 times
the number 3. Using the std::random() function does not guarantee
you that after 50 extractions you have the desired number of replications
for each number. My solution is to provide a class, which does that:
unsigned int Size() const;
unsigned int> > RndList;
... // some more stuff
The Init member function receives a std::vector<unsigned
int> which represents the number of replications that one
wants to generate for each element of the vector. In our case 10,
25 and 15.
Its body is:
RandomListInt::Init(const std::vector<unsigned int>&
for (int i = 0; i < items.size(); i++)
for (int c = 0; c < items[i];
For each of the 3 elements a number of items equal to the number
that they represents are stored in a std::list with associated
a randomly generated number through a std::pair. Then, the
list is sorted depending on the associated random number.
Calls to the GetNext() member will return the index of the
vector passed to the Init(...) function and remove the pair
from the list.
int val = RndList.front().first;
When the RndList is empty (that is when Size() returns 0),
the randomization is completed.
An example of its use is the following one, where GetEventsCount()
and events are members of a SimpleProtocol class of mine, and rnd_list
is an instance of RandomListInt:
for (unsigned int i = 0; i < GetEventsCount();
..// randomization completed!