On 08.12.2016 14:07, Stefan Ram wrote:
> Ben Bacarisse <
ben.u...@bsb.me.uk> writes:
>>> What I show in the classroom is:
>>> ::std::srand
>>> ( ::std::time( 0 )* ::std::time( 0 )+ ::std::time( 0 ));
>>> so as to shift some of the variation from the least-
>>> significant digits of the time to some more significant
>>> digits of the result.
>> And thereby introducing some predictable structure into the seed (for
>> example the bottom bit in now known with high probability). What
>> evidence so you have that is generally a good thing to do?
>
> I did not claim that it is /generally good/.
> But when one just does
>
> ::std::srand( ::std::time( 0 )); ::std::cout << ::std::rand();
>
> , one can see that the value printed one second later
> (when the whole program is being started anew)
> is near to the value printed one second earlier.
> E.g., subsequent runs of the program might print
> 5811, 5912, and 5984. The multiplication will make
> this obvious correlation disappear (under many
> implementations).
You're seeing the time values. Just advance the generator one number.
Successive pseudo-random numbers have no easily discernible relation to
each other.
> The above multiplication makes this effect disappear
> /in the course room/ so the participiants can see that
> they can exert some control over it. I do not claim
> that this is generally a good thing - generally one would
> not use »::std::rand()« at all!
>
>> That seems odd too. The all of the values of std::rand() are probably
>> related to the seed in an obvious way.
>
> It has the same effect of making the obvious correlation
> go away.
>
> In the course room, the participiants are always like,
> "But those values all are near to each other!«, and I
> want to show them that it is possible to change this.
I recommend Knuth's “The Art of Computer Programming”, I think it was vol I.
Well, that's the only book about random generators I can recommend,
because it's they only one I've read. :)
Cheers & hth.,
- Alf