Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:54:45+00:00 2026-06-10T09:54:45+00:00

I have taken to selecting random values from enums like so: import std.random :

  • 0

I have taken to selecting random values from enums like so:

import std.random : uniform;
import std.stdio : writefln;
import std.conv;

enum E {A, B, C}

int main(){
    auto select = cast(E)uniform(to!int(E.min), to!int(E.max));
    writefln("select %s", select);
    return 0;
}

This is surprisingly verbose, and prone to issues if any enum members take values outside the default (or larger than int).

Ideally I would take a range that represents the elements of the enum, and provide this to randomSample. However, this doesn’t appear to be possible.

Is there a more idiomatic way to select a random value from an enum in D?

EDIT:

Using the answer provided by fwend, here is a template function that achieves what I want:

T RandomEnumElement(T)() if (is(T == enum)){
    auto members = [EnumMembers!T];
    return members[(uniform(0, members.length))];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:54:46+00:00Added an answer on June 10, 2026 at 9:54 am
    import std.random : uniform;
    import std.stdio : writefln;
    import std.conv;
    import std.traits;
    
    enum E {A, B, C}
    
    int main(){
        auto select = [EnumMembers!E][uniform(0, 3)];
        writefln("select %s", select);
        return 0;
    }
    

    Edit: if you need to use the enum values more than once, you can store them in a static immutable array first, otherwise the array will be built every time. That also allows you to get rid of the magic number 3.

    (...)
    int main(){
        static immutable Evalues = [EnumMembers!E];
        auto select1 = Evalues[uniform(0, Evalues.length)];
        writefln("select %s", select1);
    
        auto select2 = Evalues[uniform(0, Evalues.length)];
        writefln("select %s", select2);
        return 0;
    }
    

    Edit 2: As pointed out by Idan Arye, the template could be even terser:

    T RandomEnumElement(T)() if (is(T == enum)){
        return [EnumMembers!T][(uniform(0, $))];
    }
    

    Edit 3: tgehr has suggested the following solution, which would build the lookup table once at compile time and avoid GC allocation altogether:

    T RandomEnumElement(T)() if (is(T == enum)) {
        static immutable members = [EnumMembers!T];
        return members[uniform(0, $)];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to build a query string based on values taken from 5 groups
I have a complicated MySQL query which takes a lot of time, selecting from
I have taken a manifest from one of the answers posted here in SO
I have taken over a project that was build from suggestions in this Dan
From my previous question for selecting specific html text, I have gone through this
I have no idea why this isn't working. I've taken code from a previous
I have text files with a lot of uniform rows that I'd like to
I want to show a random record from the database. I would like to
I have created one MVC3 application. In which I'm selecting two Ids from two
I have taken this little snippet straight out of some code I'm working on:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.