Here is the code that doesn’t work:
Enemy.strength = srand((unsigned)time(NULL)) % 10;
Enemy.strength is an int
I did some research and i found you can’t directly define a variable with rand/srand
such as:
a = rand();
I am just wondering why and if there is a way around this or what alternative you suggest
Language: C… not C++
srand(seed)returns void. It is for seeding the random number generator.rand()returns a pseudo-random integer between 0 and RAND_MAX (defined in stdlib.h).So to get a random strength for your enemy you should do something like:
You can place a call to
srandsomewhere in your code, but it only needs to be called once. It should be called before any calls to rand().