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