#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'M having a little trouble understanding the program above. The function rand_0toN1 returns rand() % n, in this case I believe that it's returning a random number from 1-10 because 10 was passed as an argument through n, which is not the same n that was declared at the beggining of the program but local to the function only. DId I get that part right.
Secondly was going on with these two lines?

hits[r]++;

&

cout<< static_cast<double>(hits[i]) / (n / 10)

Thanks.

Close. he functions returns a value from 0 to n-1.

hits[r]++;

hits is an array. Increment the r th element of hits

cout<< static_cast<double>(hits[i]) / (n / 10)

Break it into it's pieces based on the parens and you'll see. Or be more specific in what you don't understand.

Edited 4 Years Ago by WaltP

The function rand_0toN1 returns rand() % n, in this case I believe that it's returning a random number from 1-10 because 10 was passed as an argument through n, which is not the same n that was declared at the beggining of the program but local to the function only. DId I get that part right.

Yes.

Secondly what's going on with these two lines?

The line hits[r]++; increments the hit-count for the value r.

The expression static_cast<double>(hits[i]) / (n / 10) simply computes the probability of getting the number i times the range, and from the number of trials that you did. If the random number generator is perfectly uniform, then this number should be very close to 1. Also, the cast in the expression is there to force the expression to be evaluated as a floating-point division, as opposed to an integer division (which would discard the remainder).

So

 hits[r]++

is adding 1 to whatever "r" is in this case. What is the relevence of r in the program? I don't see it everying printed out or used in any way in the cout<< statements.

Ok, so let's recapitulate what the program does. The program generates random numbers between 0-9 and records the number of times each specific number was obtained. To record the hit-count for each specific number, it uses an array called hits. The main loop generates N random numbers, each temporarily stored in the variable called r. For each sample, it increments the hit-count for the specific number obtained. Doing hits[r] accesses the hit-count associated to the specific number r. Doing hits[r]++ increments that hit-count. The variable r is only a temporary variable used within an iteration of the main loop.

At the end, the program outputs the number of hits for each specific number, and also the corresponding probability (normalized).

The point of the program is just to verify that the random-number generator is indeed uniform (there is equal probability for any number to come up).

So

hits[r]++

is adding 1 to whatever "r" is in this case. What is the relevence of r in the program? I don't see it everying printed out or used in any way in the cout<< statements.

No. It increments the r th value of the array hits. IOW, if r=3, hits[3] will get incremented. r will not change at all.

Edited 4 Years Ago by WaltP

This article has been dead for over six months. Start a new discussion instead.