1. /*
  2.  
  3.   Physics 420 March 24, 2009
  4. HW #8, Problem #1
  5.  
  6. ------------------------------------------------
  7. Initial Inputs:
  8.  
  9. Calculate the near-neighbor correlation 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. int k = 3; // 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. cout << " " << rand_x[i] << " " << moment<< endl;
  43. }
  44.  
  45. for(int i = 0; i < nmax, i++)
  46. {
  47. moment = moment + rand_x[i]*rand_x[i+k];
  48. }
  49.  
  50. // Calculates k'th moment
  51. final_moment = (1.0 / nmax) * moment;
  52.  
  53. cout << endl << k << "th moment is: " << final_moment << endl << endl;
  54.  
  55. system("pause");
  56. return 0;
  57. }
  58.