Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
25 replies to this topic

#21 Álvaro   Crossbones+   -  Reputation: 12918

Like
1Likes
Like

Posted 27 December 2012 - 06:15 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.

Sponsor:

#22 Bacterius   Crossbones+   -  Reputation: 8521

Like
0Likes
Like

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#23 BeerNutts   Crossbones+   -  Reputation: 2812

Like
0Likes
Like

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.

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)

#24 Servant of the Lord   Crossbones+   -  Reputation: 18497

Like
0Likes
Like

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] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#25 Sik_the_hedgehog   Crossbones+   -  Reputation: 1605

Like
0Likes
Like

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.

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.

#26 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

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


Edited by Cornstalks, 27 December 2012 - 11:31 PM.

[ 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 ]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS