Skip to content
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

Fixing issue 7936 #542

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 31 additions & 10 deletions std/random.d
Expand Up @@ -1498,13 +1498,8 @@ struct RandomSample(R, Random = void)
/** /**
Constructor. Constructor.
*/ */
static if (hasLength!R)
this(R input, size_t howMany)
{
this(input, howMany, input.length);
}


this(R input, size_t howMany, size_t total) private void initialize(R input, size_t howMany, size_t total)
{ {
_input = input; _input = input;
_available = total; _available = total;
Expand All @@ -1515,6 +1510,34 @@ Constructor.
prime(); prime();
} }


static if(is(Random == void))
{
static if (hasLength!R)
this(R input, size_t howMany)
{
this(input, howMany, input.length);
}

this(R input, size_t howMany, size_t total)
{
initialize(input, howMany, total);
}
}
else
{
static if (hasLength!R)
this(R input, size_t howMany, Random gen)
{
this(input, howMany, input.length, gen);
}

this(R input, size_t howMany, size_t total, Random gen)
{
this.gen = gen;
initialize(input, howMany, total);
}
}

/** /**
Range primitives. Range primitives.
*/ */
Expand Down Expand Up @@ -1608,17 +1631,15 @@ auto randomSample(R)(R r, size_t n) if (hasLength!R)
auto randomSample(R, Random)(R r, size_t n, size_t total, Random gen) auto randomSample(R, Random)(R r, size_t n, size_t total, Random gen)
if(isInputRange!R && isUniformRNG!Random) if(isInputRange!R && isUniformRNG!Random)
{ {
auto ret = RandomSample!(R, Random)(r, n, total); auto ret = RandomSample!(R, Random)(r, n, total, gen);
ret.gen = gen;
return ret; return ret;
} }


/// Ditto /// Ditto
auto randomSample(R, Random)(R r, size_t n, Random gen) auto randomSample(R, Random)(R r, size_t n, Random gen)
if (isInputRange!R && hasLength!R && isUniformRNG!Random) if (isInputRange!R && hasLength!R && isUniformRNG!Random)
{ {
auto ret = RandomSample!(R, Random)(r, n, r.length); auto ret = RandomSample!(R, Random)(r, n, r.length, gen);
ret.gen = gen;
return ret; return ret;
} }


Expand Down