Hello, I got a problem. Whenever I use rand() and I rerun the source
I get the same results each time do u know whats wrong?

put srand(time(0)) at the beginning of your main. Make sure
to include "#include<ctime>".

I agree. The reason for this is, that a computer can not just generate a random number. It does not have emotions to pick a number, it must solve it mathematically. There is no equation not actually giving the same each time.

So you have to seed the rand function with the current time. Then in theory tho, rand would return the same if the time is the same each time you run it. Although that shouldn't be the case, since I believe the time function returns the time since 1970 or something.

I already knew that about the "randomness". But random means
an event not able to be predicted by a human. Although I tried this
but when I rerun the program works fine but when I loop on it it doesn't :S

The random number generator has a specific starting value and follows a mathematical formula to generate the next number. If you know the starting value and the formula, you can predict the series that it will generate. That's just the way computerized random number generators are, some are better than others.

What you need to remember is that you have to "seed" the random number generator with a random value to set the starting point. The most reliable way to do this is using srand((unsigned)time(0)); at the beginning of your code. The value of time() is one of the few things in computing that has enough variation in it to simulate true randomness.

Post the section of your code that generates and displays your random numbers. We should be able to tell you what's going on then...

This is the part of the code that I got the problem with.

while(a!=7)
          {
          a++;
                    again:
                          srand(time(0));
          counter=rand()%(18-a);
          counter++;
          rcounter=rand()%(73-a);
          rcounter++;
          status=rand()%2;
if(!status)
{
          if(enemyreal[counter][rcounter+a]==' ')
          enemyreal[counter][rcounter+temp]=177;
          else
          goto again;
}
else if(status)
{
          if(enemyreal[counter+a][rcounter]==' ')
          enemyreal[counter+temp][rcounter]=177;
          else
          goto again;
}

When I goto again: because the status "random" value is not
the one I want to display it doesn't generate a new one but
instead it keeps the same value.

srand. once. at the beginning.

Move the srand() to the very beginning of your program, you only need to do it once for each time the program runs, not for each iteration of your "loop", or every time you use rand().

I suspect that as fast as modern computing equipment runs you are repeatedly re-seeding the random number generator to the same starting value each time. This would have the effect of causing the generator to produce the same 3 numbers repeatedly. The value of time only changes (I believe) once every second (but it may be every millisecond). You can use rand() thousands, if not millions, of times before it needs to be re-seeded.

After you have done that, give us another update.

[edit] oops.... Dave beat me to it.[/edit]

Im surprised no one has reamed you for this yet, but don't use goto. Throw it in a nested loop, or restructure it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.