-4

Hi I have 3 Strings that I need to be randomly chosen.

HeLlo
hELLo
HElLo

I need a simple code to randomly choose between those three options.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    there are about one trillion duplicates of this question is you take the time to search for them. – njzk2 May 06 '15 at 17:51

3 Answers3

1

Make an array of your strings.. Then choose randomly

String[] array = String[]{"HeLlo","hELLo","HElLo"};
    array[(int)(Math.random()*3)]
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
1

This makes an array of Strings and choses a random index from 0 to 2.

Random r = new Random();
String[] list = {"HeLlo", "hELLo","HElLo"};
System.out.println(list[r.nextInt(3)]);
Bitcoin M
  • 1,206
  • 8
  • 24
1

Java Code

import java.util.Random; 
public class TestProgram {

    public static void main(String[] args) {

        String[] myStringArray = {"HeLlo","hELLo","HElLo"};
        Random generator = new Random();
        int randomIndex = generator.nextInt(myStringArray.length);
        System.out.println(myStringArray[randomIndex]);
    }   

}
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17