1. /*
  2.  
  3. Initial Inputs:
  4.  
  5. Calculate the 3-rd moment of the random number distribution
  6.  
  7. Output
  8.  
  9. Outputs random numbers to screen
  10.  
  11. */
  12.  
  13.  
  14. // generate integer random numbers between i1 and i2
  15. #include <iostream>
  16. #include <cstdlib>
  17. #include <cmath>
  18. #include <ctime>
  19.  
  20. using namespace std;
  21. int main ()
  22. {
  23.  
  24. int nmax=100; /* generate 10 random numbers*/
  25. int i1=1, i2=6, irandom;
  26. int rand_x[nmax];
  27. double k = 3.0; // degree of moment
  28. int moment = 0;
  29. double final_moment = 0.0;
  30.  
  31.  
  32. srand (469); /* initial seed for x-values*/
  33. //srand(time(NULL)); // better to "randomize" seed values
  34.  
  35. for (int i=0; i < nmax; i=i+1)
  36. {
  37. rand_x[i] = i1+rand()%(i2-i1+1); //number between i1 & i2*/
  38.  
  39. cout << " " << rand_x[i]<< endl;
  40. }
  41.  
  42. for(int i=0; i < nmax; i++)
  43. {
  44. moment = moment + pow(rand_x[i],k);
  45. }
  46.  
  47. // Calculates k'th moment
  48. final_moment = (1.0 / nmax) * moment;
  49.  
  50. cout << endl << k << "rd moment is: " << final_moment << endl << endl;
  51.  
  52. system("pause");
  53. return 0;
  54. }
  55.