mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
refs #481 Algorithms to copy random elements from containers.
This commit is contained in:
@@ -9,8 +9,11 @@
|
||||
|
||||
#include "samplesalgorithm.h"
|
||||
#include "blackmisc/algorithm.h"
|
||||
#include "blackmisc/sequence.h"
|
||||
#include "blackmisc/collection.h"
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <numeric>
|
||||
|
||||
namespace BlackMiscTest
|
||||
{
|
||||
@@ -20,6 +23,41 @@ namespace BlackMiscTest
|
||||
*/
|
||||
int CSamplesAlgorithm::samples()
|
||||
{
|
||||
BlackMisc::CSequence<int> seq;
|
||||
for (int i = 1; i <= 100; ++i)
|
||||
{
|
||||
seq.push_back(i);
|
||||
}
|
||||
const int samples = 200;
|
||||
BlackMisc::CSequence<int> means;
|
||||
{
|
||||
for (int i = 0; i < samples; ++i)
|
||||
{
|
||||
auto randoms = seq.randomElements(10);
|
||||
means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
|
||||
}
|
||||
int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
|
||||
int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0, [ & ](int a, int n) { return a + (n - mean) * (n - mean); }) / samples);
|
||||
qDebug() << "randomElements";
|
||||
qDebug() << "means:" << means;
|
||||
qDebug() << "mean of the means:" << mean;
|
||||
qDebug() << "std deviation of the means:" << stdDev;
|
||||
}
|
||||
means.clear();
|
||||
{
|
||||
for (int i = 0; i < samples; ++i)
|
||||
{
|
||||
auto randoms = seq.sampleElements(10);
|
||||
means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
|
||||
}
|
||||
int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
|
||||
int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0, [ & ](int a, int n) { return a + (n - mean) * (n - mean); }) / samples);
|
||||
qDebug() << "sampleElements";
|
||||
qDebug() << "means:" << means;
|
||||
qDebug() << "mean of the means:" << mean;
|
||||
qDebug() << "std deviation of the means:" << stdDev;
|
||||
}
|
||||
|
||||
QStringList src { "a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3" };
|
||||
std::random_shuffle(src.begin(), src.end());
|
||||
qDebug() << src;
|
||||
|
||||
Reference in New Issue
Block a user