1

I am trying to take this char array here:

char[] options = {'F','Z','P','E','N','T','L','C','D','O'};

and generate a new random char array of a specific length. Like this:

char[] results ={'Z','E','L','C'...} all the way up to a length of 70 characters long. I've already tried to create a new char such as char[] results = new char[70] and then using a for loop to try to get this. But for some reason my mind is blanking. Can anybody refresh me? Thanks all

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Ethan
  • 1,905
  • 2
  • 21
  • 50

2 Answers2

4

Kind of straightforward solution

char[] options = {'F','Z','P','E','N','T','L','C','D','O'};
char[] result = new char[70];
Random r=new Random();
for(int i=0;i<result.length;i++){
    result[i]=options[r.nextInt(options.length)];
}
zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
3
private static char[] options = {'F','Z','P','E','N','T','L','C','D','O'};

public static char[] createRandomArray() {
    Random r = new Random();

    char[] arr = new char[70];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = options[r.nextInt(options.length)];
    }
    return  arr;
}
zr0gravity7
  • 2,917
  • 1
  • 12
  • 33