Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program to statistically estimate a number LIM given NUM numbers in the range 0-LIM.
- Test is repeated REP times and standard deviation is displayed.
- (Uses the Boost Random library)
- */
- #include <boost/random.hpp>
- #include <iostream>
- #include <math.h>
- #include <time.h>
- int boost_rand(int lower, int upper)
- {
- static boost::mt19937 rng(time(NULL));
- boost::uniform_int<> myRange(lower, upper);
- boost::variate_generator<boost::mt19937&, boost::uniform_int<> > myRand(rng, myRange);
- return myRand();
- }
- int main(int argc, char *argw[]) // argument: 1 => print result of every test
- {
- using namespace std;
- int set;
- if(argc > 1)
- set = argw[1][0] - 48;
- else set = 0;
- int i, j, lim, rep, num, val, max = 0;
- float calclim, sd, sd_percent;
- cout<<"\nEnter LIM: ";
- cin>>lim;
- cout<<"\nEnter REP: ";
- cin>>rep;
- cout<<"Enter NUM: ";
- cin>>num;
- sd = 0;
- for(i=0; i<rep; i++)
- {
- max = 0;
- for(j=0; j<num; j++)
- {
- val = boost_rand(0,lim);
- if(max < val)
- max = val;
- }
- calclim = max + (float)max/num - 1;
- if(set)
- cout<<"\nMax: "<<max<<", Calc: "<<calclim<<"\n";
- sd = sd + pow((calclim - lim),2);
- }
- sd /= rep;
- sd = sqrt(sd);
- sd_percent = (sd/lim) * 100;
- cout<<"\nStandard deviation: "<<(int)sd<<" ("<<sd_percent<<"\%)\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment