Choosing a random value from an enum

How do I do it? I'm trying to pick a random value from an enum for an object at the creation of that object.

Do I have to set that enum's value in the create event (to zero) and then make it random in a step event? Or can I set that random value right away in the create event.

And, what is the actual syntax? I can't find it anywhere.

value = random(enum); ??

Doesn't seem to work for me.

As always, thanks!!
 

kupo15

Member
you can't really do that and I'm not entirely sure why you want to? Perhaps you aren't using enums as intended? Enums mainly are simply to count up in order, even though you can assign values to them. Are you assigning values to your enums? If you aren't, then a nice trick you can do is stick "enumcount" as the last entry in your enum list. Then if you want to pick a random number in the enum simply do irandom(enum.enumcount);

This won't work if you assign values to your enums though, bc enums are supposed to be used as simple ordering and if you are doing so, doing having irandom = 3 would be the same thing as manually getting the vale of enum.4th_entry. But if your 4th entry in the enum is set to 20, the irandom() will return 3 (as in the 4th counted up from 0) instead of 20 which you assigned it to be

Also, in your example you used random() which includes decimals instead of irandom() which uses integers only.
 
So here's what I want to do.

I want to create objects that have first and last names. I was going to set up an enum for each, and simply list a bunch of names in the enum. Then, when the object is created, a first and last name would be randomly picked from the enum list.

enum firstName {
Bill,
Tom,
Sam
}

Something like this. Not doable?
 

kupo15

Member
Its doable but not with enums....why would you use enums instead of a list or an array? I think you are using enums for the wrong thing (as much as I would love to be able to have a function that gets the value of an enum like enum_get_value(firstName,"Bill")

What I think you don't realize is that Bill = 0, Tom = 1 etc...you can't supply a value and get the name associated with that. This is not what you use enums for, you should be using arrays/lists instead

GML:
firstName[0] = Bill;
firstName[1] = Tom;
firstName[2] = Sam;

etc..

// then you can do

var size = array_length(firstName);
var name = firstName[irandom(size)];
 

kupo15

Member
both ds_list and array's will work just fine. If you wanted to use ds_lists you can type them out somewhat like arrays by using the | accessor:

var name = firstName[| irandom(size)]

instead
 
Enums can only store integers, so I wouldn't recommend using them for names, but to provide a solution to the question the topic poses, choosing a random value from an enum is simple if you use the last entry in your enum as an endpoint marker.

GML:
enum Class {
    fighter,
    rogue,
    mage,
    priest,
    paladin,
    samurai,
    total
}

class = irandom(Class.total-1);
EDIT: Forgot that irandom is inclusive.
 
Last edited:

samspade

Member
Enums can only store integers, so I wouldn't recommend using them for names, but to provide a solution to the question the topic poses, choosing a random value from an enum is simple if you use the last entry in your enum as an endpoint marker.

GML:
enum Class {
    fighter,
    rogue,
    mage,
    priest,
    paladin,
    samurai,
    total
}

class = irandom(Class.total);
irandom is inclusive, so that could actually return total as a result (same for the array example). But I agree with that I would use enums and would use an array of strings if what you want is names.
 
Top