Hi all,
I’m sure this can be done; but I can’t get it too work.
I wish to generate a random number/index from an enum range; so:
public enum gem_types
{
value10,
value50,
value100,
value500
}
random.range(gem_types.value10,gem_types.value500)
Obviously doesn’t work; but I am looking for a value between 0 and 3 (or indeed value10 value500)
Currently I hard code random.range(0,3) which is pants and asking for trouble later when I forget about it :-)))
Cheers
Do the values ever change or do you want them to? Enums are backed by integers so you can use them as such but you can’t directly assign them integer values. If you need to associate an Enum with an integer value then I would do a Dictionary with the enum as the key and the int as the value.
Found it 
static T GetRandomEnum<T>()
{
System.Array A = System.Enum.GetValues(typeof(T));
T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
return V;
}
3 Likes
Hmmmmmm,
Now I am thinking how do I ‘weight’ the random number in a nice way?
Currently each have a 25% chance of being chosen; it would be nice to have (for example) value500 has a 1 in a hundred chance of being selected and value50 has a 1 in 25 chance etc. etc.
Cheers
I think my own weighted array is simpler e.g.
{value10,value10,value10,value50 etc. etc.}

Cheers