0

I am trying to generate random numbers (1 to 10) using enum. The variable "num" is not getting the updated random value, not sure what mistake I am making. Any pointer will be helpful. Thank you.

Java Code:

    enum RandomNumbers
   {

     ONE, 
     TWO, 
     THREE, 
     FOUR,
     FIVE, 
     SIX, 
     SEVEN, 
     EIGHT, 
     NINE, 
     TEN;

    public static RandomNumbers getRandom()    
    {
      return values()[(int)(Math.random() * values().length)];
    }
   }


    public class RandomNumbersEnum
    {

     public static void main(String[] args)
     {

      RandomNumbers randN = RandomNumbers.getRandom();

      int num = 0;


      if (randN.values().equals("ONE"))
         num = 1;
      else if(randN.values().equals("TWO"))
         num = 2;
      else if(randN.values().equals("THREE"))
         num = 3;
      else if(randN.values().equals("FOUR"))
         num = 4;
      else if(randN.values().equals("FIVE"))
         num = 5;
      else if(randN.values().equals("SIX"))
         num = 6;
      else if(randN.values().equals("SEVEN"))
         num = 7;
      else if(randN.values().equals("EIGHT"))
         num = 8;
      else if(randN.values().equals("NINE"))
         num = 9;
      else if(randN.values().equals("TEN"))
         num = 10;
      System.out.println("The random number is: " + num);
     }
   }
4
  • 1
    Anything wrong about java.util.Random.nextInt(9) + 1; ? Commented Nov 22, 2013 at 19:02
  • 2
    @assylias, that generates the range of 1-9, not 1-10 :) Commented Nov 22, 2013 at 19:06
  • 1
    Yeah, this is just... awkward Commented Nov 22, 2013 at 19:06
  • @kviiri you're a right. Commented Nov 22, 2013 at 20:30

2 Answers 2

2

You can try to use the Random class of Java, i let you some example:

Random r = new Random();
int number = r.nextInt(10)+1; 
System.out.println(number); 
Sign up to request clarification or add additional context in comments.

2 Comments

The OP specified the range was one to ten (inclusive based on the question), not zero to nine. Otherwise a good, concise answer though.
Thanks Juan. Can Random class be used with enum type?
1

The reason that this doesn't work is that your if statements are testing whether the array that enumerates all RandomNumbers instances is equal to some String. Of course, it never could be. When you call randN.values(), you are actually invoking the static method RandomNumbers.values(), the same method you used in your getRandom() method. If you want a string that you can compare, randN.name() would work, but it's really not a good idea.

If you insist on comparing something, you should use an identity comparison, like this:

int num; /* Notice I'm not prematurely assigning meaningless garbage here. */
if (randN == RandomNumbers.ONE)
  num = 1;
else if(randN == RandomNumbers.TWO)
  num = 2;
else if(randN == RandomNumbers.THREE)
  num = 3;
else
  throw new IllegalArgumentException("Unrecognized RandomNumber: " + randN);

The next step forward would be to use a switch statement:

int num;
switch(randN) {
  case ONE: num = 1; break;
  case TWO: num = 2; break;
  case THREE: num = 3; break;
  default: throw new IllegalArgumentException();
}

You aren't making good use of an enum. Here's an approach that takes full advantage of an enum type:

public enum RandomNumber {

  ONE(1),
  TWO(2),
  THREE(3);

  private final int value;

  private RandomNumber(int value)
  {
    this.value = value;
  }

  public int getValue()
  {
    return value;
  }

  public static RandomNumber roll()
  {
    RandomNumber[] values = values();
    int idx = ThreadLocalRandom.current().nextInt(values.length);
    return values[idx];
  }

  public static void main(String... argv)
  {
    RandomNumber num = RandomNumber.roll();
    System.out.printf("The random number is %s (%d).%n", num, num.getValue());
  }

}

4 Comments

Thanks Erickson. I've tried your first solution and it worked. I was not comparing the right value I understand. I will now try the last two techniques you've shown. Thank you.
The last solution shows the following compile time error message: RandomNumber2.java:3: error: constructor RandomNumber2 in enum RandomNumber2 cannot be applied to given types; ONE(1), ^ required: no arguments found: int reason: actual and formal argument lists differ in length
Did you copy and paste the code? I think the problem is due to your renaming of the type, and perhaps forgetting to update the name everywhere, or perhaps you didn't accurately copy the constructor, etc. The code in my answer is lacking only imports.
I think mistake is mine, I did not copy, changed my other code to fit this technique instead. I will check it again. Thanks Erickson.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.