Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

Picking a random value from an Enum

If you want to get a random value from an Enum in Java, you can use the following code:

Grades.values()[new Random().nextInt(Grades.values().length)];

You can define `Grades` like this:

public enum Grades{
    A,
    B,
    C,
    D,
    E
}
Share the knowledge!
GHajba

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below

%d bloggers like this: