The intent of this program is to help answer the question “How good is the random number generator rand()?”. The program must be designed and written in a modular fashion using functions and arrays

The function rand() returns int values between 0 and some system-specified maximum value. The first part of the problem is to discover that maximum value by two different methods: compute it, or just retrieve it. The actual value is given by the constant RAND_MAX which is defined in the header file <cstdlib>.

The computed value could be estimated by making repeated trials and retaining the largest value seen in those trials, but it’s not clear how many trials you would need to get the right answer. So, the first part of the project is to make 1,000 trials and see how close to RAND_MAX you actually came. This will require a function whose sole parameter is the number of trials.

The values from rand() are supposed to be uniformly (evenly) distributed between 0 and RAND_MAX. The second part of the project is to test the claim that the values rand()%n are uniformly distributed between 0 and n-1. To do this, count the number of occurrences of each value of rand()%n as you make RAND_MAX trials.

This will require a loop over the number of trials, and an array to do the counting. Print the array to cout when finished. Do this for n = 2, 10, 16, 100 and 128. This will require a function whose sole parameter is n.

There are many situations where you need to generate random points in a square. As a simple example, consider two consecutive values a, b obtained from rand(). How often do these combinations occur?
a and b are both even,
a is even, b is odd,
a is odd, b is even,
a and b are both odd.

The third part of the project is to test the claim that all four combinations occur equally often. You should make RAND_MAX/2 trials (each trial uses two calls to rand()). This will require a function with no parameters, but you could generalize the test from 2 x 2 to n x n (still considering only two consecutive values).
Before starting each series of tests, be sure to restart the random number sequence by calling srand(0). This will ensure consistent results.

Here is what I have;

#include <iostream>
#include <cstdlib>
using namespace std;

int generatehighvalue (int trials);
void countoccurrence(int trials);
void main ()
    int highestgenerated;
highestgenerated = generatehighvalue(1000);
cout <<"difference we got is"<<RAND_MAX - highestgenerated<<endl;
cout<<"hishest possible is"<<RAND_MAX<< "and highest generated is"<<highestgenerated<<endl;

int generatehighvalue (int valuetrials)
    int num, compare;
    num = rand();
    compare = 0;
    if (trials > 1)
        compare  = generatehighvalue(trials - 1)
        if (compare > num)
            return compare;
            return num;

void countoccurence(int maxinum)
    int occurence[128];
    memset (occurence,0,sizeof(occurence));
    if (maxnum > 128)
        cout <<"max number cannot be more than 128"<<endl;

        return ;
for(int trials = RAND_MAX; trials>0; trials--)

I dont really understand what to do next... to match the sample output of this;

We used srand(time(0)) to initialize the calls to rand(). You should
use srand(0) instead. You should not expect to get the same numbers as
these, but the totals should agree, and the formatting should be similar.

Only a few examples are shown here. While you are trying to get the
output into a nice format, it would be easiest to skip the cases of n =
100 and 128.

Some people with access to Linux have gotten very different results.
In particular, RAND_MAX is much larger there, so the program takes longer
to run.


After 1000 trials, max rand() seems to be 32698
RAND_MAX = 32767

Testing rand()%n for n = 2
0 16219
1 16548
missing 0 of 2

Testing rand()%n for n = 10
0 3255
1 3357
2 3204
3 3334
4 3191
5 3267
6 3274
7 3260
8 3295
9 3330
missing 0 of 10

Testing rand()%n for n = 16
0 2073
1 1983
2 2030
3 2123
4 2030
5 2021
6 1951
7 1992
8 2067
9 2052
10 2053
11 2118
12 1998
13 2222
14 2017
15 2037
missing 0 of 16

count[0][0] = 4009
count[0][1] = 4107
count[1][0] = 4094
count[1][1] = 4173

count[0][0] = 1826
count[0][1] = 1794
count[0][2] = 1860
count[1][0] = 1803
count[1][1] = 1818
count[1][2] = 1875
count[2][0] = 1805
count[2][1] = 1818
count[2][2] = 1784

10 Years
Discussion Span
Last Post by Ancient Dragon

you posted the requirements, you posted the code. Now, please edit your post to use code tags so that we do not go blind trying to read all that unformatted code.

>>I dont really understand what to do next... to match the sample output of this;
I think you were told you may not get exactly the same output.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.