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
  • 19,939
  • 11
  • 82
  • 96
Ethan
  • 1,844
  • 2
  • 20
  • 47

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,308
  • 1
  • 6
  • 22
Antoniossss
  • 28,922
  • 5
  • 49
  • 91
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,308
  • 1
  • 6
  • 22