Send to a Friend

Mariah's avatar

Can you help me understand why my code is wrong?

Asked by Mariah (25883points) January 26th, 2013

This is homework. Please don’t write any code for me.

I am programming in C and I am supposed to fill an array with random numbers. There should be two separate functions – one that generates a random number, and one that calls the other to fill an array.

My function that generates a random number is working fine. I know because I compiled it and ran it a few times and got different numbers. Yet, when I run the second function, I end up with an array containing all the same number. This number changes when I run it again, but all the numbers are always the same.

The basic outline of my second function is:

int myArray[size];
for(int i=0; i<size; i++){
myArray[i] = generateRandomNumber();
}

I think this might be happening because the random numbers are generated using the computer’s clock, and the program runs through them so fast that the clock doesn’t change and so all the numbers turn out the same. Do you think that might be what’s happening, or is it something on my end? If that’s what’s happening, what the heck should I do about it?

Using Fluther

or

Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.