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:

<br />
Grades.values()[new Random().nextInt(Grades.values().length)];<br />

You can define `Grades` like this:

<br />
public enum Grades{<br />
    A,<br />
    B,<br />
    C,<br />
    D,<br />
    E<br />
}<br />

Share the knowledge!

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: