
AMDG Robert Ramey wrote:
Steven Watanabe wrote:
This is only a major problem if you explicitly pass the seed: boost::minstd_rand gen(42);
can't this the be trapped and an an exception thrown?
The code above is correct under any version of seeding. boost::minstd_rand gen(42); boost::minstd_rand gen2(2147483647 + 42); // gen and gen2 are the same Of course its possible to throw an exception. I'm not asking what's possible. I'm trying to determine what the correct behavior ought to be. In the case above, the user is explicitly passing the seeds and can easily make sure that the seeds match any preconditions of seed. Therefore in this case, the ideal behavior is not to do any fixing up. In the cases below, the user does not have as much control over the seed. Thus, it is much easier to accidentally violate preconditions. This wouldn't matter so much in another library. But the seed for a PRNG is usually going to be random. As a result, forbidding some seeds is likely to lead to intermittent failures.
If the seed comes from somewhere else: boost::minstd_rand gen(time(0)); then either the library or the user has to fix it up.
Currently, the statement boost::minstd_rand gen(time(0)); will work almost all the time.
However, it is wrong because it will fail when time(0) returns 2147483647.
again, can't this be trapped?
Only when it happens--which is very rare.
Is it worse to fix things up and possibly have multiple seeds yield that same sequence or to let code like this fail randomly once in a while? It seems to me that this bug is likely to be harder to trace because it won't even be reproducible.
Isn't it possible trap the problem and throw an exception?
To summarize, I would like any library I use to either work as advertised or notify me that it can't handle the arguments. And I would like this notification to be as soon as possible. If it can't do this, then how can I know that my program will really work?
seed will work as advertised. Regardless of the outcome of this discussion, I will make sure that the exact behavior is documented. In Christ, Steven Watanabe