``````#include <Stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

int rand_0toN1(int n);

int hits;

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.

## All 7 Replies

Are you confused because the output is not a double (with some decimal places) or because of the number of digits?

The output isn't a double with decimals because your program is doing integer division, not double division. Typecase either the numerator or denominator to double and it will do double division. Typecasting the entire result to double doesn't do the same thing.

Number of digits. When I look at the could I would expect each humber in that second column to simply be a number between 1 & 10.

If n, the number of trials is, say, 50,000, you would be randomly generating anumber from 0 to 9, inclusive, 50,000 times, so the hits[] array should sum to 50,000. With ten elements in the hits[] array, the expected value for each element would be 50,000 divided by 10, or 5,000, 4 digits. Exactly what you are seeing.

You have ten buckets. You are incrementing a bucket 50,000 times (once every trip through the loop), so the sum of those buckets needs to be 50,000. If they were all less than ten, that would add up to 90 at the most, a far cry from 50,000. The double versus integer division is in the third column, which is apparently not what you are concerned with.

I havent seen and of the numbers that even. I just ran the program and on the first line 0: it have me 1965. How did it come up with that number? I thought I just explained above. Those values make perfect sense. It appears the second row adds up to 20,000 and all the entries are about the same, which is what is to be expected. The third row all hovers around 1, also to be expected. It means that your random number generator appears is working pretty well. The first row is 0 through 9. 1,965 is the number of times out of 20,000 that rand0_toN1 returned 0.

If the random numbers it gave me where based up on my input dived by a random number between 0 and 9 you can not get a number like 1965, or most any of the number it gave me in that column. I tried it with my calculator. Sorry, that picture was to small.

You tried WHAT with your calculator? 1965 is not based on any division at all. I'm not sure why you think it is. Column 3 is Column 2 divided by (n / 10), which is 2000 in your case, so column 2 (1965) divided by 2000 becomes .9825 in column 3.

Column 2 needs to add up to 20,000 and average out to 2,000 and it does. Column 3 needs to average out to 1 and add to 10, and it does, barring any roundoff error.

You have 20,000 balls and you have 10 buckets to randomly place those balls in. Think of a Pachinko machine with ten slots at the bottom, each slot having a bucket at the bottom. Stick a ball in at the top. It will randomly fall into one of the ten buckets. Repeat this process 19,999 more times. Now take the buckets out of the machine and count the balls. Bucket number 0 has 1965 balls in it. That's how to visualize this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.