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:

1. The initial count of roaches for each house is a random number between 10 and 100.
2. Each week, the number of roaches increases by 30%.
3. 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.
4. 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 < 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 << "The number of roaches in House A is " << totalRoachesInHouseA << endl;
cout << "The number of roaches in House B is " << totalRoachesInHouseB << endl;

if ((week == 4) || (week == 8)) // It's extermination time!
{
totalRoachesInHouseA = totalRoachesInHouseA - roachesInHouseA * .9;
totalRoachesInHouseB = totalRoachesInHouseB - roachesInHouseB * .9;
}
else if (totalRoachesInHouseA > totalRoachesInHouseB) // Migration from
//House A to HouseB
{
temporaryHouseA = totalRoachesInHouseA * .3;
totalRoachesInHouseA = totalRoachesInHouseA - temporaryHouseA;
totalRoachesInHouseB = totalRoachesInHouseB + temporaryHouseA;
}
else if (totalRoachesInHouseA < totalRoachesInHouseB) // Migration from
// House B to House A
{
temporaryHouseB = totalRoachesInHouseB * .3;
totalRoachesInHouseB = totalRoachesInHouseB - temporaryHouseB;
totalRoachesInHouseA = totalRoachesInHouseA + temporaryHouseB;
}
}
}
• You want random numbers that equal a specific set of numbers? That does not sound random but rather like an oxymoron. Nov 8 '11 at 4:39
• I realized that. Let me post the project requirements so you understand what I'm trying to get.
– Earl
Nov 8 '11 at 4:40

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;

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

for (week = 1; week < N; week++) {
// 2. each week, each house's population increases
increase_pop(houses);
increase_pop(houses);

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

// 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);
show(houses);
}
}

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 or houses 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.

Don't use global variables.

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

It is better to use local variables and pass them as parameters to functions. Having global variables (better know as mutable global state) makes it hard to change the program from other situations (also known as tight coupling).

Also don't declare multiple variables on a single line (don't be lazy, one variable per line).

Declare variables close to the point where you are going to use it.
Here you are declaring temporaries like a million miles from their usage point.

int temporaryHouseA = 0; // storage for arithmetic
int temporaryHouseB = 0; // storage for arithmetic

What is the difference between totalRoachesInHouseX and roachesInHouseX

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

This gives you a number between 10 and 110. Not quite what you want.

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

To check for things that happen every 4 weeks try:

(weeks % 4) = 0 /* Or any constant from 0 -> 3 */

I don't think the exterminator and the roach moving are exclusive. The roaches move even if the exterminator comes.

Also Its 30% of the difference (not 30% of the total).

As we have two houses exactly the same you may want to use an array rather than separate variables. This allows you to make things more flexible.

Handling Random

rand() returns a random number from a sequence (ie it returns the current value from the sequence then increments to the next position). The sequence is initiated by srand() which sets the start point of the sequence (note adding one to the input does not move it one place down the sequence its a bit more fancy). Normally you would initiate srand() like this:

srand(time(NULL)); // Call ONLY once just after main() starts

But for testing it is nice to get the same random numbers each time you run. To do this initiate the random sequence at the same point.

srand(0);  // Initiate the rand at point 0 in the sequence.