refs #452, allow to pick random elements from collection

(will be used in view resizing)
This commit is contained in:
Klaus Basan
2015-09-23 15:17:44 +02:00
committed by Mathew Sutcliffe
parent 5718abe03c
commit ad1938b8d8
3 changed files with 40 additions and 1 deletions

View File

@@ -20,7 +20,6 @@
#include <iterator>
#include <utility>
#include <initializer_list>
#include <QtConcurrent/QtConcurrent>
namespace BlackMisc
{
@@ -519,6 +518,23 @@ namespace BlackMisc
return sorted(BlackMisc::Predicates::MemberLess(key1, keys...));
}
/*!
* \brief Return some random elements from container
* \param elements how many elements
*/
CSequence randomElements(int elements) const
{
if (this->isEmpty() || elements < 1) { return CSequence(); }
int high = this->size();
CSequence r;
for (int i = 0; i < elements; i++)
{
int randomIndex = qrand() % high;
r.push_back(this->operator [](randomIndex));
}
return r;
}
//! Equals operator.
friend bool operator ==(const CSequence &a, const CSequence &b)
{