Why is it "bad" to call srand multiple times?
Crossbones+ - Reputation: 11532
Posted 27 December 2012 - 06:21 PM
In response to Bacterius's post I want to say that the rand() implementation you get in Linux (i.e., GLIBC's implementation) these days is much much better than a simple LCG.
That's good to know. Better be safe than sorry though. I liked the C++11 way of having multiple standard PRNG's with a common interface so you could choose which one you wanted (I think there was the LCG, a MT, a couple lagged generators and a few other simple ones) and could easily change with a simple token replace.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Crossbones+ - Reputation: 3594
Posted 27 December 2012 - 07:08 PM
EDIT: Never mind, this is in general programming. Carry on.
Edited by BeerNutts, 27 December 2012 - 07:09 PM.
---(Old Blog, still has good info): 2dGameMaking
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Crossbones+ - Reputation: 26036
Posted 27 December 2012 - 07:51 PM
In general, using rand() is perfectly fine for basically everything unless you're running a gambling site, ultra high precision probabilistic algorithm, cryptographic infrastructure, or other highly specialized tasks. Nothing like information overload to put somebody off programming.
Using rand() is perfectly fine *if* you only need....
I completely agree that rand() is less-than perfect in many many situations. If the rand() implementation varies from compiler to compiler (which I think was the case was the C++98 standard, but I might be mistaken here), then that's yet another reason why you'll need your own implementation, so (for example) generated map seeds can be shared over the internet with users using your game compiled with different compilers on different operating systems.
My only point (directed at the OP's 'why such a simple response'), was that rand() works perfectly fine for a console game or a tetris/asteroids clone, and that anyone not understanding when and why to call srand() more than once, is also not needing anything better than rand(), and probably wouldn't understand how something like a Mersenne Twister implementation works.
Thankfully, C++11 has new better psuedo-random number generators (such as built-in standardized mersenne twister, among others), though because their usage is all templatey and classy, which is great for intermediate and advanced C++ users, a beginner will probably still use rand().
Discussion among more experienced developers (like in this thread) about the nitpicky implementation details is of immense value and very educational to people who didn't go through college (like myself). It just overwhelms people still trying to understand the difference between references and pointers, and possibly drives them away from programming because "it's too confusing".
Edited by Servant of the Lord, 27 December 2012 - 08:16 PM.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | [Fly with me on Twitter]
Crossbones+ - Reputation: 2196
Posted 27 December 2012 - 10:49 PM
One thing I didn't see anybody here mention (unless I skipped something) is that calling srand with the same value to get the same sequence is not portable. Sure, if you run it on the same implementation, you'll get the same sequence, but the algorithm used by rand is completely implementation defined, and switching to a different implementation of the standard library is bound to give you a different sequence.
Calling srand more than once is theoretically fine but you need to be aware of how it works. It's very unlikely you'll have any serious reasons to do it, and if it's to get the same sequence as in the above case, you probably want to use either your own RNG or a RNG library which guarantees the algorithm will stay the same always.
EDIT: just realized a part of the post above says this =/ But yes, that pretty much renders calling srand again mostly useless.
Edited by Sik_the_hedgehog, 27 December 2012 - 10:50 PM.
Crossbones+ - Reputation: 7003
Posted 27 December 2012 - 11:29 PM
Good discussion on rand(), people.
I just want to add that this is in General Programming and not For Beginners, so I think it's fine if people want to get into some more of the nitty-gritty details (well, it's not the Math forum, so maybe don't go overkill on the math ). If, instead, this discussion is being intended for beginners, perhaps it should be in that forum instead... Just trying to encourage some good discussion.
Edited by Cornstalks, 27 December 2012 - 11:31 PM.