Random numbers

The following code can be used to generate pseudo-random floating point numbers in $ [0,1]$ .

#include<iostream>
#include<sys/time.h>

int main(int argc, char** argv) {
  using namespace std;

  // initialize randomizer
  struct timeval theTV;
  struct timezone theTZ;
  gettimeofday(&theTV, &theTZ);
  srandom(theTV.tv_usec);

  // pick a random number between 0 and 1
  double normalizedRndCoeff = (double) random() / (double) RAND_MAX;
  cout << normalizedRndCoeff << endl;
  return 0;
}
Write a small program that generates a random sample of given size $ N$ (input from command line) and that computes the mean and the variance. Use your program to show empirically that

$\displaystyle \alpha=\lim_{N\rightarrow\infty}$   Var$\displaystyle (X) \cong 0.083,$    

where $ X$ is a random variable. Prove that $ \alpha=\frac{1}{12}$ .

Optional: update your program to generate a random sample whose elements are within given numerical bounds.



Subsections

Leo Liberti 2008-01-12