Confusion about `Random`

H. S. Teoh hsteoh at qfbox.info
Fri Dec 23 00:00:06 UTC 2022


On Thu, Dec 22, 2022 at 08:17:56PM +0000, jwatson-CO-edu via Digitalmars-d-learn wrote:
> On Thursday, 22 December 2022 at 17:33:48 UTC, Paul Backus wrote:
> > So, as far as I can tell, there is nothing wrong with your code, and
> > the random number generator is working as intended.
> > 
> > Most likely you have made a mistake somewhere in the part of the
> > code that you did not post, and that mistake is what's causing the
> > lack of randomness you observed in the output.
> 
> Right, the entire project is about 2k lines, so I didn't post it.
> I've isolated the problem to instances when my program is interpreting
> a loop.  Somehow the loop context must be storing an old seed for
> `rnd`?  I'm still searching for the issue and I have not been able to
> replicate it in smaller example.
[...]

You could try using DustMite to reduce it to a minimal (or at least
smaller) example.

My personal guess is that you forgot a `ref` somewhere when you pass the
RNG to a function.  Given that due to historical accident std.random
uses structs for RNG implementations, and this can sometimes lead to
unexpected results when you unintentionally passed an RNG state by value
instead of by reference.  One thing to try could be to scan all your
function signatures where an RNG is passed, and make sure there's a
`ref` on it.


T

-- 
Give a man a fish, and he eats once. Teach a man to fish, and he will sit forever.


More information about the Digitalmars-d-learn mailing list