/*
Brian Wieland Physics 420 March 24, 2009
HW #8, Problem #1
------------------------------------------------
Initial Inputs:
Calculate the 3-rd moment of the random number distribution
Output
Outputs random numbers to screen
*/
// generate integer random numbers between i1 and i2
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int main ()
{
int nmax=100; /* generate 10 random numbers*/
int i1=1, i2=6, irandom;
int rand_x[nmax];
double k = 3.0; // degree of moment
int moment = 0;
double final_moment = 0.0;
srand (469); /* initial seed for x-values*/
//srand(time(NULL)); // better to "randomize" seed values
for (int i=0; i < nmax; i=i+1)
{
rand_x[i] = i1+rand()%(i2-i1+1); //number between i1 & i2*/
cout << " " << rand_x[i]<< endl;
}
for(int i=0; i < nmax; i++)
{
moment = moment + pow(rand_x[i],k);
}
// Calculates k'th moment
final_moment = (1.0 / nmax) * moment;
cout << endl << k << "rd moment is: " << final_moment << endl << endl;
system("pause");
return 0;
}