forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomize.cpp
34 lines (28 loc) · 911 Bytes
/
randomize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <random>
#include "Complex.hpp"
template<typename T>
void compute(int len, T initial, T step) {
// allocate vectors
std::vector<T> v(len+1), diffs(len+1);
// fill and randomize v
std::generate(, , [](...) { ...; });
std::shuffle(..., std::default_random_engine{});
// compute differences
std::adjacent_difference(...);
// compute standard deviation of all differences
const T sum = std::reduce(...);
const T sumsq = std::accumulate(..., [](...) { ...; });
const T mean = sum/len;
const T variance = sumsq/len - mean*mean;
std::cout << "Range = [" << initial << ", " << step*len << "]\n"
<< "Mean = " << mean << '\n'
<< "Variance = " << variance << '\n';
}
int main() {
compute(1000, 0.0, 7.0);
// call compute here with Complex
}