❔ How to get a random enum value?
I have a function here where I would like to get a random value from an enum type. The goal is to give NextValue any enum type, and it will calculate its length and return a random value. The cast on return isn't working. Is this possible to do?
18 Replies
you want GetValues, not GetNames
iirc its
return (T)(object)Rand.Next
and also that
well, no
okay
Appears to be working:
that
I got this message:
did you get the edit I made
I swapped it from T : Enum to T : struct
I did.
remove the - 1
or ull never get the last value
no that just doesn't work
Same thing.
It's a custom Random class I made, so last value is max - 1.
this works for me
ah okay
make the constraint
unmanaged, Enum
Seems to be working. Thanks!
unmanaged is new to me.
I think
struct, Enum
might also work?
unmanaged just means there are no objects in it, which applies so enums, so that's what I'm used to usingworks
Ah, I see.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.