Jump to page: 1 2 3
Thread overview
[Issue 9339] New: std.random.uniform!Enum should return random enum member
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 18, 2013
Andrej Mitrovic
Jan 22, 2013
Andrej Mitrovic
Jan 22, 2013
Andrej Mitrovic
Feb 13, 2013
Andrej Mitrovic
Feb 13, 2013
Andrej Mitrovic
January 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339

           Summary: std.random.uniform!Enum should return random enum
                    member
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: hsteoh@quickfur.ath.cx


--- Comment #0 from hsteoh@quickfur.ath.cx 2013-01-17 14:33:56 PST ---
Currently, std.random.uniform does not respect enum bounds:

import std.random;
import std.stdio;

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

void main() {
        writefln("%d", Fruit.min);
        writefln("%d", Fruit.max);

        writeln(uniform!Fruit());
}


Typical output:

14
47
cast(Fruit)-2046817621


It should at the very least respect the enum range defined by the enum's .min and .max properties (which in this case are 14 and 47, respectively).

Ideally, it should only return one of the four possible values of Fruit, not anything outside the range of .min and .max, and not anything in between the four possible values.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339


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

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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@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: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #2 from hsteoh@quickfur.ath.cx 2013-01-17 16:40:15 PST ---
Yeah, that will do. Except that the "enum arr = [EnumMembers!E];" line may run into issue 6057. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339


bearophile_hugs@eml.cc changed:

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


--- Comment #3 from bearophile_hugs@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: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #4 from bearophile_hugs@eml.cc 2013-01-17 16:48:48 PST ---
(In reply to comment #1)

>     enum arr = [EnumMembers!T];

Be very careful with enum arrays. They are very inefficient.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #5 from hsteoh@quickfur.ath.cx 2013-01-17 16:52:13 PST ---
(In reply to comment #3)
[...]
> 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)];
> }

You're right, we want the array to be statically initialized.

Does enum arr = [...] cause the code to create the array every time the function is called?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-17 17:01:43 PST ---
(In reply to comment #4)
> (In reply to comment #1)
> 
> >     enum arr = [EnumMembers!T];
> 
> Be very careful with enum arrays. They are very inefficient.

Well, the compiler is very inefficient, static will do.

> 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)];
> }

That's not doing what was requested.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-17 17:08:20 PST ---
As much as I'd love to make a pull for this I already know I'm going to run into Issue 6057 (which has a pull but needs a review).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #8 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-17 17:29:52 PST ---
(In reply to comment #7)
> As much as I'd love to make a pull for this I already know I'm going to run into Issue 6057 (which has a pull but needs a review).

Looks like I said the same thing as Comment #2. :p

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #9 from hsteoh@quickfur.ath.cx 2013-01-17 17:36:18 PST ---
If you write "static arr = [EnumMembers!T];", you should be able to evade issue 6057.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3