[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:47:24 PST 2013


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


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #3 from bearophile_hugs at eml.cc 2013-01-17 16:47:21 PST ---
(In reply to comment #1)

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


I think this is more efficient:


T uniform(T)()
if (is(T == enum) && isIntegral!T || isSomeChar!T)
{
    static immutable T[EnumMembers!T.length] members = [EnumMembers!T];
    return members[std.random.uniform(0, members.length)];
}

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