Why is it "bad" to call srand multiple times?

Started by
24 comments, last by Cornstalks 11 years, 3 months ago
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.
Advertisement
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.”

EDIT: Never mind, this is in general programming. Carry on.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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".

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

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 tongue.png ). If, instead, this discussion is being intended for beginners, perhaps it should be in that forum instead... Just trying to encourage some good discussion.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement