- Home /
What is the best way to pick random value form enum in other class?
first, I have a enum class Card
public class Card{}
public enum CardType{
AAA,
BBB,
CCC,
DDD,
EEE
}
So what do I want to do, is use a method CardGenerate in another class, let's say PlayCard.cs, and generate a card with random card type from the enum.
So what is the best way to do it?
Although this could be achieved by set simple array and random from elements, but it just looks like not so professional.
BTW, what's the best usage for enum?
thanks.
Answer by chillersanim · Jun 23, 2013 at 01:09 PM
If you kn. the exact length of the enum, you can generate a random value with the Random class. Then you make. wraper class that gets you the entry of the enum depending on the value.
I wouldn't reccomend an enum, better use a static array and then you can use the lengt and a direct acces without a wraper.
If you want to modify the Array during runtime, I would reccomend a list from System.collections.generics, that gives you more control over adding and removing Elements.
Greetings
Chillersanim
Can you please explain why you downvoted this post? Is it unusefull, incorrect or what?
Your answer
Follow this Question
Related Questions
Add random amount of random items from one list to another? 2 Answers
Bool Temporary Variable in Enum c# 1 Answer
two Random.Range returning similar values 1 Answer
Random movement script not working C# 2 Answers
Random Button / Plane Generation. 0 Answers