   Problem with srand()
  Hey guys, I need some help here.My problem is, I'm trying to use srand to give a more dynamic numbers compared to just use rand(). But I found out that if I use the srand, the numbers given sometimes maybe unstable, and sudden jump.Maybe you don't understand what exactly I mean to, here's an example://In a game, if I collected a coin, then the coin will be spawned at another place, the position of the new place will be generate randomly.//each collected coin will be rewarded with 1 score.srand((unsigned int)time(0));float x;float y;x = (float)( rand() % 200 );y = (float)( rand() % 200 ) * -1;setPosition(x, y);So just wanna ask is there any problem with the code above?Because if I uses the code, sometimes, even I just collected 1 coin but the score may suddenly increase a lot (like suddenly from 1 to 10)... Just wanna know why it would happen?PS: If you I set srand first then everything is working fine.   
  Erm, you're calling srand once, right? Because you're just supposed to seed it once per program run.I don't see any code there that deals with the scoring, so no idea why that's jumping.   ;  Huh, the srand just need call once?Then I think I've wrong because I call it again whenever the coin has been collected.The code for the score is just score++;lol   ;  After I change and call the srand once and it's work now :DThanks   ;Quote:Original post by birdkingzAfter I change and call the srand once and it's work now :DThanksNot having seen the actual code used, I can't be positive, but having made this mistake a few times I have a good guess as to what is happening:1) time(0) returns the time in seconds2) rand() will return the same sequence of numbers given the same seed value.: srand(time(0)) will seed the same sequence of numbers no matter how many times it is called, until a whole second has passedIf you were to collect a coin, your score would implement by 1, and then a new coin is generated. It is possible that the new coin could generate in a position near enough to the last coin that you would collect it immediately. Then, srand(time(0)) would be called again right away - and if less than a second has passed, time(0) would return the same time as it did before, therefore seeding with the same value used before. The following calls to rand() would produce the same output they did previously, causing a coin to generate exactly where it did before.This, of course, would cause you to collect the new coin immediately - and then a new coin would be generated in the same spot again. This would continue to happen until you moved far away enough from that spot, or until a whole second has elapsed.I could easily see this cycle happening several times in a second.   ;  So is that mean I should use something like time(10) instead of time(0) ?   ;  No, time takes a pointer. 0 is a valid argument (the null pointer), but 10 isn't.   ;Quote:Original post by birdkingzSo is that mean I should use something like time(10) instead of time(0) ?Nope, just listen to Washu and call srand(time(NULL)) a single time: during your initialization routine. My point earlier was that repeatedly seeding with the same number was the likely cause of your problem.   ;  There is nothing random about rand. It is pseudo-random (a mathematically generated sequence of numbers). srand seeds the random number generator. A random number generated with the same seed value (the parameter into srand) will always return the same sequence of numbers.Because time returns in seconds, you continuously get the same sequence of values when calling rand after srand is called. This most likely allows you to collect multiple coins at the same location if you get there before the next second elapsed.By only calling srand at the start of your program (on initialization) you get pseudo-random behaviour rather than getting the same values repeatedly on many occasions.   