Skip to content

Instantly share code, notes, and snippets.

@alekseyl1992
Created June 17, 2014 16:23
Show Gist options
  • Save alekseyl1992/2de3975f88b009b4e53d to your computer and use it in GitHub Desktop.
Save alekseyl1992/2de3975f88b009b4e53d to your computer and use it in GitHub Desktop.
Normal distribution from uniform distribution
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <vector>
#include <algorithm>
#include <array>
float getVal() {
float t2 = 0;
int n = 1000;
for (int i = 0; i < n; ++i) {
t2 += float(rand()) / float(RAND_MAX);
}
t2 /= n;
return (t2 - 0.5)*sqrt(12*n);
}
int main()
{
srand(time(0));
//generate seq
std::vector<float> seq;
for (int i = 0; i < 300; ++i)
seq.push_back(getVal());
//analize
auto minmax = std::minmax_element(seq.begin(), seq.end());
auto constexpr zonesCount = 10;
std::array<int, zonesCount> stat = {0};
auto zoneSize = (*minmax.second - *minmax.first)/(zonesCount-1);
for (auto el: seq)
stat[(el - *(minmax.first))/zoneSize]++;
//output
for (auto el: stat) {
for (int i = 0; i < el; ++i)
std::cout << "o";
std::cout << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment