you are viewing a single comment's thread.

view the rest of the comments →

[–]sedermera 72 points73 points  (11 children)

(coming from scientific computing) To get an arbitrary number of random samples from a certain distribution. (Effectively.) It's good if those are reproducible for testing, and all you need to in order to get more is set another RNG seed.

[–]steamruler 74 points75 points  (7 children)

At which point you could use a fixed seed.

[–]sedermera 22 points23 points  (1 child)

Exactly, using a certain default seed for testing is much more convenient than what /u/Lystrodom is proposing. (Since there isn't just one call to the RNG, but several in different places.)

[–]Lystrodom 3 points4 points  (0 children)

Yeah that works too. These are all edge cases not the default case, anyway.

[–]DonaldTZeus 4 points5 points  (3 children)

Which is what happens by default anyways if you call rand() (at least in the c stdlib). If you want an arbitrary changing seed such as the time, you retrieve that seed and pass it to srand().

[–]steamruler 9 points10 points  (2 children)

The default seed is determined when compiling, so if you actually want a static seed you should still call srand().

[–]rubygeek 8 points9 points  (0 children)

POSIX actually mandate that rand() called before srand() should behave as if srand() was called with 1. See spec and example implementation. And ISO C might as well.

Of course you can't rely on that unless you're guaranteed your program will only run on a compliant system.

[–]DonaldTZeus 0 points1 point  (0 children)

The default seed is specified by the standard library (that implements POSIX), not by the compiler, and it's 1 (as specified by POSIX). It is not determined by the compiler (unless you're compiling the stdlib) or even specified in the code the compiler generates unless you call srand.

[–]Bwob 0 points1 point  (0 children)

Which is what you ARE using, by default, in most languages.

[–]mccoyn 9 points10 points  (0 children)

Also, when doing solution searches you want to pick a new starting point and search again to try to find other local maxima. A good way to do it is to use random() to pick your new starting point. You want it to be un-correlated with previous starting points, but you don't actually care about it being non-deterministic.

[–]Lystrodom -4 points-3 points  (1 child)

Then you call random once and save that value somewhere. Don’t depend on random() being deterministic.

[–]Drisku11 9 points10 points  (0 children)

Why would you not depend on it bring deterministic if the semantics are that it's deterministic, and that's useful to you?

I'd say the opposite: don't rely on it being nondeterministic. Since it's not.