Is it possible to check the length of an enum?

Suppose I have some arbitrary enum, and I’m trying to generate a random value from it. It’s trivial to accomplish this with a static cast and a hardcoded random int:

  //.h
  UENUM(BlueprintType)
 namespace EColor 
 {
     enum Type
     {
          Red,
          Green,
          Blue,
     };
 }
 
 //.cpp
     EColor::Type myColor = static_cast<EColor::Type>( FMath::RandRange(0,2) );

However, this requires me to keep excruciating track of every place I use this logic in, and manually change the range I feed FMath whenever I modify the enum. Is there any way I can manually check the exact number of possible values an enum has, instead of hard-coding it every time?

Not that I know of. There is another way to do that though, kinda hacky but works

I’m kind of surprised by this- I know UE1-3 had something called EnumCount, but intellisense doesn’t recognize this in 4 and I can’t find a reference to it in the API.

Why not create a static function as such:

const int32 Color_Count = 3;
static EColor::Type GetRandColor()
{
    return static_cast<EColor::Type>(FMath::RandRange(0, Color_Count - 1));
}

And that’s all you have to call when you need a random EColor::Type. When ever you add/remove values to/from namespace EColor, then you only need to update Color_Count.