Skip to content

Instantly share code, notes, and snippets.

@sotex
Created February 27, 2019 07:34
Show Gist options
  • Save sotex/bb634553f8b22907b7ea3f8e7b6c8a94 to your computer and use it in GitHub Desktop.
Save sotex/bb634553f8b22907b7ea3f8e7b6c8a94 to your computer and use it in GitHub Desktop.
C++11随机数等.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <random>
int main()
{
//std::copy(v.begin(),v.end(),std::ostream_iterator<string>(std::cout, "\n"));
std::default_random_engine generator;
//std::normal_distribution<double> distribution(5.0,1.0);
std::uniform_real_distribution<double> distribution(0.1,1.0);
std::vector<double> res;
for(int i=0;i<30;++i){
res.push_back(distribution(generator));
}
for(int i=3 ;i<30;++i){
//res[i] += 100.0;
}
std::sort(res.begin(),res.end());
std::copy(res.begin(),res.end(),std::ostream_iterator<double>(std::cout, ","));
std::cout<<std::endl;
std::pair<std::vector<double>::const_iterator,std::vector<double>::const_iterator> maxmin
= std::minmax_element (res.begin(),res.end());
double min = *maxmin.first;
double max = *maxmin.second;
std::cout<<"max:"<<max<<" min:"<<min;
int fc[10] = {0,0,0,0,0,0,0,0,0,0};
double sum = 0.0;
for(auto& r:res){
int i = ((r - min) * 9.99) /(max - min);
fc[i] += 1;
sum += r;
}
std::cout<<" avg:"<<sum/res.size()<<std::endl;
std::copy(std::begin(fc),std::end(fc),std::ostream_iterator<int>(std::cout, "|"));
std::cout<<"\n(max - min) = "<<(max - min)<<std::endl;
double rs = 0.0;
for(auto& r:res){
int i = ((r - min) * 9.99) /(max - min);
rs += 1.0/r /double(fc[i]);
}
rs /= 30;
rs = 1.0/rs;
std::cout<<"rs = "<<rs<<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment