Barrier Barrier - 1 year ago 37
Android Question

How to get a specific random number out of three numbers in android

I have three numbers (25, 30 and 35). I want to get one random number out of these three numbers.

I know how to get a random number from a range, but in this case I have no idea how to get (randomly) one of those three numbers.

Does anyone have any idea?

Thank you.

Answer Source

As Ollie said in the comments, use an array and get the random value from that array.

package main.application;

import java.util.Random;

public class Main {

    public static void main(String[] args) {

        int[] intArray = {25, 30, 35};

        int idx = new Random().nextInt(intArray.length);
        String random = "Value is " + (intArray[idx]);