Skip to content

Commit

Permalink
Fixes Issue 9339 - Uniform for enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejMitrovic authored and WalterBright committed Feb 13, 2013
1 parent 193f87a commit 128eaa9
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions std/random.d
Expand Up @@ -593,7 +593,7 @@ Parameter for the generator.
Throws:
$(D Exception) if the InputRange didn't provide enough elements to seed the generator.
The number of elements required is the 'n' template parameter of the MersenneTwisterEngine struct.
Examples:
----------------
Mt19937 gen;
Expand Down Expand Up @@ -1272,7 +1272,7 @@ if (isIntegral!T || isSomeChar!T)

/// Ditto
auto uniform(T)()
if (isIntegral!T || isSomeChar!T)
if (!is(T == enum) && (isIntegral!T || isSomeChar!T))
{
return uniform!T(rndGen);
}
Expand All @@ -1289,6 +1289,26 @@ unittest
}
}

/**
Returns a uniformly selected member of enum $(D E).
*/
auto uniform(E)()
if (is(E == enum))
{
static immutable E[EnumMembers!E.length] members = [EnumMembers!E];
return members[std.random.uniform(0, members.length)];
}

unittest
{
enum Fruit { Apple = 12, Mango = 29, Pear = 72 }
foreach (_; 0 .. 100)
{
auto f = uniform!Fruit();
assert(f == Fruit.Apple || f == Fruit.Mango || f == Fruit.Pear);
}
}

/**
Generates a uniform probability distribution of size $(D n), i.e., an
array of size $(D n) of positive numbers of type $(D F) that sum to
Expand Down Expand Up @@ -1342,15 +1362,15 @@ unittest
}

/**
Partially shuffles the elements of $(D r) such that upon returning $(D r[0..n])
is a random subset of $(D r) and is randomly ordered. $(D r[n..r.length])
will contain the elements not in $(D r[0..n]). These will be in an undefined
order, but will not be random in the sense that their order after
$(D partialShuffle) returns will not be independent of their order before
Partially shuffles the elements of $(D r) such that upon returning $(D r[0..n])
is a random subset of $(D r) and is randomly ordered. $(D r[n..r.length])
will contain the elements not in $(D r[0..n]). These will be in an undefined
order, but will not be random in the sense that their order after
$(D partialShuffle) returns will not be independent of their order before
$(D partialShuffle) was called.
$(D r) must be a random-access range with length. $(D n) must be less than
or equal to $(D r.length).
or equal to $(D r.length).
*/
void partialShuffle(Range, RandomGen = Random)(Range r, size_t n,
ref RandomGen gen = rndGen)
Expand Down

0 comments on commit 128eaa9

Please sign in to comment.