Program with random number generation I am having trouble with the random number generator. For week 0 only, I want to set the starting number of roaches in House A (totalRoachesInHouseA) to 97 and the number of roaches in House B (totalRoachesInHouseB) to 79. Then, for weeks 1 to 10, I want to generate random numbers for both houses. I am trying to get the same numbers in the example simulation below.

Initial Population: Week 0: House 1 = 97; House 2 = 79
Week 1: House 1 = 119; House 2 = 109
Week 2: House 1 = 151; House 2 = 144
Week 3: House 1 = 194; House 2 = 189
Week 4: House 1 = 25; House 2 = 247
Week 5: House 1 = 118; House 2 = 235
Week 6: House 1 = 198; House 2 = 260
Week 7: House 1 = 280; House 2 = 314
Week 8: House 1 = 37; House 2 = 395
Week 9: House 1 = 187; House 2 = 374
Week 10: House 1 = 315; House 2 = 414


Here are the instructions for the project: 


  Write a program which keeps track of the number of roaches in two
  adjacent houses for a number of weeks. The count of the roaches in the
  houses will be determined by the following:
  
  
  The initial count of roaches for each house is a random number between 10 and 100.
  Each week, the number of roaches increases by 30%.
  The two houses share a wall, through which the roaches may migrate from one to the other. In a given week, if one house has more roaches
  than the other, roaches from the house with the higher population
  migrate to the house with the lower population. Specifically, 30% of
  the difference (rounded down) in population migrates.
  Every four weeks, one of the houses is visited by an exterminator, resulting in a 90% reduction (rounded down) in the number of roaches
  in that house. Your implementation must use functions and local
  variables.
  


Here's what I have so far:

int roachesInHouseA, roachesInHouseB; //my two houses.
int temporaryHouseA, temporaryHouseB;
double totalRoachesInHouseA, totalRoachesInHouseB; //the total count of houses. 
int week;


int main( )
{
    int temporaryHouseA = 0; // storage for arithmetic
    int temporaryHouseB = 0; // storage for arithmetic

    int totalRoachesInHouseA = 97; // test number for A
    int totalRoachesInHouseB = 79; // test number for B

    roachesInHouseA = rand() % 100 + 10;
    roachesInHouseB = rand() % 100 + 10;

    //The functions declaring the random count of roaches in both houses between 
    //10 and 100.
    for (int Z = 1; Z &lt; 12; Z++) // My for loop iterating up to 11 weeks.
    {
        totalRoachesInHouseA = totalRoachesInHouseA + roachesInHouseA * .3; 
        totalRoachesInHouseB = totalRoachesInHouseB + roachesInHouseB * .3; 

        // My function declaring that the roach population explodes by 30% weekly.
        cout &lt;&lt; "The number of roaches in House A is " &lt;&lt; totalRoachesInHouseA &lt;&lt; endl;   
        cout &lt;&lt; "The number of roaches in House B is " &lt;&lt; totalRoachesInHouseB &lt;&lt; endl;   


        if ((week == 4) || (week == 8)) // It's extermination time!
        {
            totalRoachesInHouseA = totalRoachesInHouseA - roachesInHouseA * .9;
            totalRoachesInHouseB = totalRoachesInHouseB - roachesInHouseB * .9;
        }
        else if (totalRoachesInHouseA &gt; totalRoachesInHouseB) // Migration from  
        //House A to HouseB 
        {
            temporaryHouseA = totalRoachesInHouseA * .3;
            totalRoachesInHouseA = totalRoachesInHouseA - temporaryHouseA;
            totalRoachesInHouseB = totalRoachesInHouseB + temporaryHouseA;
        }
        else if (totalRoachesInHouseA &lt; totalRoachesInHouseB) // Migration from  
        // House B to House A
        {
            temporaryHouseB = totalRoachesInHouseB * .3;
            totalRoachesInHouseB = totalRoachesInHouseB - temporaryHouseB;
            totalRoachesInHouseA = totalRoachesInHouseA + temporaryHouseB;
        }
    }
}
First of all, the requirements are specific in requiring that you "use functions". I'm pretty sure the intent of that was the exact opposite of how you've structured the code, with everything in main.

You might want to consider some (more or less) top-down style development, where you write a simple main that has practically nothing except calls to lower-level functions. I'd do this by thinking of a short (word or two) summary describing each step in the specification, and use those as function names.

int main(void) { 
    house_t houses[2];

    // 1. put initial population in each house:
    populate(houses[0]);
    populate(houses[1]);

    for (week = 1; week &lt; N; week++) {
        // 2. each week, each house's population increases
        increase_pop(houses[0]);
        increase_pop(houses[1]);

        // 3. each week, a percentage of the population may migrate between the houses
        do_migration(houses[0], houses[1]);

        // 4. every four weeks, an exterminator visits one of the houses
        if (week % 4 == 0)
            exterminate(random_choice(houses, 2));

        // 5. Presumably we want to show the data for each house weekly.
        show(houses[0]);
        show(houses[1]);
    }
}


A few things about that code may be open to question -- for example, the spec doesn't seem to specify how to choose which house is exterminated at any given time. I've used a random_choice, but perhaps you think strict alternation makes more sense. In any case, however, The important point is that what we see here should be about as close as possible to a direct translation of the steps outlined in the requirement into the syntax of the target language. From there, we can define the sub-parts in relative isolation from each other.

Right now, this makes life much easier for your teacher: it's fairly easy to look through main and figure out whether it fits the requirements. He can then glance through each of the functions and see whether that function does what it's supposed to as well.

I'd also note that the same basic idea applies when school is over and you're writing real code. The big difference here is that you will spend a lot of time doing what the teacher is doing now, reading and analyzing code, not just writing it. In school, you tend to write code and (almost) never look at it again. In real life, you live with the code after you've written it.

I suspect that you have the idea of writing the code and getting it working, then breaking it up into functions. I'd advise against that. It makes the job considerably more difficult.

I'd also note how this changes the situation if (for example) you need/want to change the parameters. For example, assume you have 5 (or 50) houses in a row, and wanted to run the situation for a year instead of 10 weeks. With the code as I've written it above, that would mostly mean rewriting most of the code that explicitly refers to houses[0] or houses[1] to instead use loops and refer to houses[i]. Don't try to go overboard with this idea though. If you can make the code more flexible like this without adding extra complexity, great. At the same time, don't spend (much) extra time or work writing against possible changes in requirements that may never happen.