RandomSample with specified random number generator

Jonathan M Davis jmdavisProg at gmx.com
Thu Jun 14 15:17:31 PDT 2012


On Thursday, June 14, 2012 23:37:05 jerro wrote:
> Issues with copying rngs are not unique to randomSample - anyone
> who uses random number generators will run into them
> sooner or later. So the problem is not that randomSample takes
> a rng as a parameter - it's that rngs are value types. The
> proper solution would be to make them reference types.

Exactly. This has been discussed before (IIRC, there's a bug report which 
discusses it). The most straightforward solution is to change the random 
number generator ranges to classes, but that _will_ break code, because 
they're constructed completely differently.

However, it should be quite possible to make the structs reference types. The 
simplest way to do that would probably be to just move their member variables 
to a struct which they hold a pointer to rather than holding them directly as 
member variables. The only code that that would break would be code that 
relied on assigning one such range to another (or passing it to a function) 
effectively resulting in a save call. But since we're talking about random 
number generators, I have a hard time believing that that will actually break 
much of anything. It's more likely to _fix_ code (such as the problem being 
discussed in this thread).

The change could probably be effected in an hour or two without much difficulty. 
Doing something like making the state refcounted would be more of a pain, but 
it would also be quite doable if it were considered desirable enough.

- Jonathan M Davis


More information about the Digitalmars-d mailing list