[Issue 9339] std.random.uniform!Enum should return random enum member

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 17 16:31:51 PST 2013


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


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich at gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-01-17 16:31:49 PST ---
Would this suffice?

auto uniform(T)()
if (is(T == enum) && isIntegral!T || isSomeChar!T)
{
    enum arr = [EnumMembers!T];
    return randomSample(arr, 1);
}

(You would have to add a !is(T == enum) in the other template).

Example:

import std.random;
import std.stdio;
import std.traits;

enum Fruit
{
    Apple  = 14,
    Orange = 27,
    Pear   = 36,
    Mango  = 47
}

auto uniform(T)()
if (is(T == enum) && isIntegral!T || isSomeChar!T)
{
    enum arr = [EnumMembers!T];
    return randomSample(arr, 1);
}

void main()
{
    foreach (i; 0 .. 10)
        writeln(uniform!Fruit());
}

-- 
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