[Issue 8247] Inconsistent behaviour of randomSample depending on whether a random number generator is specified

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 15 01:03:39 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8247



--- Comment #5 from Jonathan M Davis <jmdavisProg at gmx.com> 2012-06-15 01:05:40 PDT ---
> Why should a RNG type have reference semantics? I think it's fine to pass them
by reference where needed.

Because it makes no sense for it to have value semantics. Take this for example

auto func(R)(R r)
{
 r.popFront();
 auto var1 = r.front;
 ///...
}

func(generator);
generator.popFront();
auto var2 = generator.front;

Both var1 and var2 will have the exact same value. This is an easy mistake to
make, and since random number generators are supposed to be returning random
numbers, having them return the _same_ number after popFront has been called is
definitely problematic.

By making them reference types, the only time that you get the same number
multiple times in a row is when you do it on purpose (e.g. by storing the value
of front or by calling save on the range).

There's a discussion on this in issue# 7067.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list