forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomize.nostl.cpp
47 lines (40 loc) · 1.1 KB
/
randomize.nostl.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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
#include <cstdlib>
constexpr auto LEN = 1000;
constexpr auto STEP = 7;
void randomize(int* v, unsigned int len) {
// we randomize via len random inversions
for (unsigned int i = 0; i < len; i++) {
int a = rand()%len;
int b = rand()%len;
int mem = v[a];
v[a] = v[b];
v[b] = mem;
}
}
int main() {
// create vector
int *v = new int[LEN+1];
for (unsigned int i = 0; i <= LEN; i++) v[i] = i*STEP;
// randomize it
randomize(v, LEN+1);
// compute diffs
int *diffs = new int[LEN];
for (unsigned int i = 0; i < LEN; i++)
diffs[i] = v[i+1] - v[i];
// compute standard deviation of it
float sum = 0;
float sumsq = 0;
for (unsigned int i = 0; i < LEN; i ++) {
sum += diffs[i];
sumsq += diffs[i]*diffs[i];
}
float mean = sum/LEN;
float stddev = std::sqrt(sumsq/LEN - mean*mean) ;
std::cout << "Range = [0, " << STEP*LEN << "]\n"
<< "Mean = " << mean
<< "\nStdDev = " << stddev << '\n';
delete[] v;
delete[] diffs;
}