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

  • Home
  • SEARCH
  • 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 8049485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:47:59+00:00 2026-06-05T06:47:59+00:00

Possible Duplicate: generating random enums Lets say I have the following: enum Color {

  • 0

Possible Duplicate:
generating random enums

Lets say I have the following:

enum Color {        
    RED, GREEN, BLUE 
};
Color foo;

What I want to be able to do is randomly assign foo to a color. The naiive way would be:

int r = rand() % 3;
if (r == 0)
{
    foo = RED;
}
else if (r == 1)
{
    foo = GREEN;
}
else
{ 
    foo = BLUE;
}

I was wondering if there was a cleaner way of doing this. I have tried (and failed) the following:

foo = rand() % 3; //Compiler doesn't like this because foo should be a Color not an int
foo = Color[rand() % 3] //I thought this was worth a shot. Clearly didn't work.

Let me know if you guys know of any better way which does not involve 3 if statements. Thanks.

  • 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-05T06:48:01+00:00Added an answer on June 5, 2026 at 6:48 am

    You can just cast an int to an enum, e.g.

    Color foo = static_cast<Color>(rand() % 3);
    

    As a matter of style, you might want to make the code a little more robust/readable, e.g.

    enum Color {        
        RED,
        GREEN,
        BLUE,
        NUM_COLORS
    };
    
    Color foo = static_cast<Color>(rand() % NUM_COLORS);
    

    That way the code still works if you add or remove colours to/from Color at some point in the future, and someone reading your code doesn’t have to scratch their head and wonder where the literal constant 3 came from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
Possible Duplicate: generating random enums I'm looking for the best way to select a
Possible Duplicate: Generating random results by weight in PHP? I have a web application
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicate: Algorithm for generating a random number is posible to generate a random
Possible Duplicate: Generating Random Numbers in Objective-C How do I generate a random number
Possible Duplicate: Java: generating random number in a range I want to generate a
Possible Duplicate: generating GUID without hyphen I am creating a GUID with the following
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Generating random numbers in C using rand to generate a random numbers

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.