#include <Stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
int rand_0toN1(int n);
int hits[10];
int main()
{
int n;
int i;
int r;
srand( static_cast<unsigned int>(time(NULL))); // set seed for random numbers
cout<< "Enter number of trials to run ";
cout<< "and press ENTER: ";
cin>> n;
// run n trials. for each trial, get a number from 0 to 9 and then
// increment the corresponding element in the hits array
for(i = 1; i <= n; i++)
{
r = rand_0toN1(10);
hits[r]++;
}
// print all the elements in the hits array, along with the ratio
// of hits to the EXPECTED hits (n / 10)
for(i = 0; i < 10; i++)
{
cout<< i << ": " << hits[i] << " Accuracy: ";
cout<< static_cast<double>(hits[i]) / (n / 10)
<< endl;
}
system("pause");
return 0;
}
// random 0-to-N1 function
// generate a random integer from 0 to N - 1
int rand_0toN1(int n)
{
return rand() % n;
}
I know I've posted this program on here in the past but I'M still having trouble understandin it. The program prints out 10 rows 0-9. Each row start with the number of that row and then a ":", it is then followed by the column that I am not understanding, just before the word Accuracy. I just don't see how it's getting the numbers for each row in the second column. When I look at the code it looks to me like the "rand_0toN1" , is just returning a random number between 1 & 10. If I input a number in the thousands each row of that column is a three digit number, but when I enter an input in the ten thousands each row is four digits. Again, when I follow that 0_toNA function all I see is a function that returns a random number between 1 & 10. If someone could help me understand I would really appreciate it. Thanks.