Sign in to follow this  
kinglamus

Help with rand()

Recommended Posts

Hello, I'm sure I will be embaressed by the answer to this question but I can't figure it out. Why, in the following section of code... when I call Attack() function each time, the number is apparently randomised in the initial call, but then it stays the same. i.e. what is displayed in my console is "5 5 5" or "10 10 10" or "6 6 6", instead of what I want which is a different number each time. #include <iostream> #include <cstdlib> #include <ctime> class Main { public: Main() {} virtual void Attack() = 0; }; class Inherit : public Main { public: Inherit() {} virtual void Attack() { srand(time(0)); int damage = ((rand() % 10) + 1); std::cout << "Damage Done: " << damage << std::endl; } }; int main () { Main * pM = new Inherit(); pM->Attack(); pM->Attack(); pM->Attack(); delete pM; pM = 0; system("PAUSE"); return 0; } Thanks if you can help, lamus

Share this post


Link to post
Share on other sites
You're seedin the random number generator with the same value each time. time() returns the time in seconds, so if you call it twice in under a second, it'll return the same value, which means you're seeding the random number generator with the same vaue, which means you get the same number sequence out.

You usually want to only call srand() once, at the start of main().

Share this post


Link to post
Share on other sites
Aha!

Of course... I can just srand() in my class constructor. I have now done so, and it works just like I wanted. Duh :).

Thanks for the help,
Lamus

Share this post


Link to post
Share on other sites
Quote:
Original post by kinglamus
Of course... I can just srand() in my class constructor. I have now done so, and it works just like I wanted. Duh :).


No, not in the class constructor.

In most cases, you want to call srand() exactly once in the entire lifetime of your program. Placing it in the class constructor will mean that it is called every time the constructor is called (and, to be honest, I can't think of any non-contrived circumstance where that would be desirable). You really do just want to call it once at the beginning of the main() function.

Share this post


Link to post
Share on other sites
Quote:
Original post by kinglamus
Aha!

Of course... I can just srand() in my class constructor. I have now done so, and it works just like I wanted. Duh :).

Thanks for the help,
Lamus


No you don't want to do that. srand() should be called exactly once in the lifetime of your program. The best place is somewhere like main. If you put it in a class constructor and then new instances of your class are constructed mid game then this will ruin the sequence of random numbers.

Share this post


Link to post
Share on other sites
Yes I see. Not thinking straight. I don't know why I was trying to seed the random no. table inside my classes, just one of those days : P

Again, thanks for the great help to you all,
Lamus

Share this post


Link to post
Share on other sites
The reason you want to only srand() once is because the function generates an entire sequence of random numbers and then advances through them each time you call the rand() function. If you seed again, you're restarting the sequence unnecessarily which will result in less random-seeming results.

But I've had a ton of trouble with srand() and rand() in the past anyway. They're not very good generators, mostly because they're designed to use little processor time, I guess. I prefer to use a separate library with a better generator, though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this