-
-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent behaviour of randomSample depending on whether a random number generator is specified #9591
Labels
Comments
joseph.wakeling commented on 2012-06-14T12:35:44ZOnline discussion on this: http://forum.dlang.org/thread/4FD735EB.70404@webdrake.net |
jens.k.mueller commented on 2012-06-14T13:41:13ZI opt for the returning the same sample (option 1). I want the sample to stay the same. |
issues.dlang (@jmdavis) commented on 2012-06-14T15:27:33ZIf you want randomSample to be consistent as to which you get, it needs to be made to handle both reference and value type random number generating ranges identically, since they could be either. At present, all of those in std.random are value types, which is actually a problem in general. They really should reference types. But regardless of which they are, there's nothing stopping someone from implementing either a value type or reference type range which is a random number generator, in which case you'll get inconsistent behavior if randomSample doesn't code for using both by using save where appropriate. |
jens.k.mueller commented on 2012-06-15T00:54:20ZNow I see why you want to pass RNG by reference. Because you may want that two functions share the same generator.
But then I would go with passing them all by reference for consistency reasons. And all functions have as default argument rndGen() which could be renamed to defaultRNG().
randomShuffle is already doing it this way. Though I don't see why it sets the template argument RandomGen to Random by default. This should be inferred automatically by the default argument rndGen() anyway.
So randomCover and randomSample should follow the same approach.
I do not see why one needs to pass a RNG by value then. Admittedly I have never used std.random. So I may have wrong use cases in mind.
But having a thread local RNG that is used by default should be okay.
@Jonathan
Why should a RNG type have reference semantics? I think it's fine to pass them by reference where needed. |
issues.dlang (@jmdavis) commented on 2012-06-15T01:05:40Z> 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. |
jens.k.mueller commented on 2012-06-15T01:22:31Z(In reply to comment #5)
> > 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.
I see. Thanks.
Since passing around RNGs should be by default by reference RNGs should be reference types. Otherwise everybody writing own functions accepting RNGs has to use ref which is error-prone.
Using ref when passing RNGs in std.random won't solve this general design issue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
joseph.wakeling (@WebDrake) reported this on 2012-06-14T12:27:53Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=8247
CC List
Description
!!!There are attachements in the bugzilla issue that have not been copied over!!!
The text was updated successfully, but these errors were encountered: