Confusion about `Random`
Steven Schveighoffer
schveiguy at gmail.com
Fri Dec 23 16:30:23 UTC 2022
On 12/23/22 10:07 AM, jwatson-CO-edu wrote:
> On Friday, 23 December 2022 at 00:58:01 UTC, Steven Schveighoffer wrote:
>> Without the rest of the code, and how random is called, I have a
>> hunch... Are you using threads by any chance?
>>
>> If, for instance, your calls to rand01 are done in a new thread, that
>> new thread will have a *default* state of Mt19937.
>
> Good question, Steve, but I do not intentionally start any threads.
> Below is the machinery that interprets a for-loop. Do you see anything
> that would enclose a previous state of the RNG?
>
Your code looks like it's making a function pointer, and that function
pointer directly uses the global RNG.
I'm not seeing how your code could be copying the RNG somehow, as I'm
assuming it's not manipulating the generated code from the compiler.
If it's not a threading problem, the only other possibility I can think
of is that your loop code is not truly calling that function over and over.
I'd start instrumenting rand01 with some printouts, and see if it's
doing what you expect. If it's not, throw and catch an exception, and
print the stack trace (or use a debugger) to help understand what is
happening.
I have been puzzled in the past with behavior that seemed to be
reasonable, but given the way the implementation happened, did
unexpected things (like caching values).
-Steve
More information about the Digitalmars-d-learn
mailing list