Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

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
}
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 0 comments
>
%d bloggers like this: