Random Number (Computer uses 0 every time)???

1
2
3
4
5
6
 case 'B':
                {int num;
                    srand(time(0));
                    num = rand() % 100+1;
                    game();
                    break;}


trying to guess a random number and it gives me 0 every time. not for sure what i'm doing wrong. I've read up on the srand and rand pertaining to the seed value but having a hard time understanding it. If I need to paste my entire source code I will. However I just copied out case b: which uses the call function game().

i'm using Xcode as a compiler if you need to know that.

Only call srand once in your program, before the first call to rand. At the beginning of main is a good place. Not sure if that will fix the problem but it should at least make rand() as random as it's supposed to be.
If your compiler supports C++11 well enough, you could use the random library.
http://cplusplus.com/reference/std/random/

Unless of course you're coding in good old C.
Topic archived. No new replies allowed.