Guest User

Vikram Menon

a guest
Sep 12th, 2009
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. /*
  2. Program to statistically estimate a number LIM given NUM numbers in the range 0-LIM.
  3. Test is repeated REP times and standard deviation is displayed.
  4. (Uses the Boost Random library)
  5. */
  6.  
  7. #include <boost/random.hpp>
  8. #include <iostream>
  9. #include <math.h>
  10. #include <time.h>
  11.  
  12. int boost_rand(int lower, int upper)
  13. {
  14.     static boost::mt19937 rng(time(NULL));
  15.     boost::uniform_int<> myRange(lower, upper);
  16.     boost::variate_generator<boost::mt19937&, boost::uniform_int<> > myRand(rng, myRange);
  17.     return myRand();
  18. }
  19.  
  20. int main(int argc, char *argw[])    // argument: 1 => print result of every test
  21. {
  22.     using namespace std;
  23.     int set;
  24.     if(argc > 1)
  25.         set = argw[1][0] - 48;
  26.     else set = 0;
  27.     int i, j, lim, rep, num, val, max = 0;
  28.     float calclim, sd, sd_percent;
  29.     cout<<"\nEnter LIM: ";
  30.     cin>>lim;
  31.     cout<<"\nEnter REP: ";
  32.     cin>>rep;
  33.     cout<<"Enter NUM: ";
  34.     cin>>num;
  35.     sd = 0;
  36.     for(i=0; i<rep; i++)
  37.     {
  38.         max = 0;
  39.         for(j=0; j<num; j++)
  40.         {
  41.             val = boost_rand(0,lim);
  42.             if(max < val)
  43.                 max = val;
  44.         }
  45.         calclim = max + (float)max/num - 1;
  46.         if(set)
  47.             cout<<"\nMax: "<<max<<", Calc: "<<calclim<<"\n";
  48.         sd = sd + pow((calclim - lim),2);
  49.     }
  50.     sd /= rep;
  51.     sd = sqrt(sd);
  52.     sd_percent = (sd/lim) * 100;
  53.     cout<<"\nStandard deviation: "<<(int)sd<<" ("<<sd_percent<<"\%)\n";
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment