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

5
  • That doesn't look like Javascript, it looks like Java. Commented May 13, 2016 at 22:00
  • Might take a look at using this lodash.com/docs#sampleSize Or look at the source if you want to roll your own solution Commented May 13, 2016 at 22:01
  • @Legumebo_Magezfeld lodash is for Javascript, his question is about Java. He got the tag wrong. Commented May 13, 2016 at 22:01
  • 1
    Please post code showing what you have tried. Commented May 13, 2016 at 22:03
  • Did you try looking at this? stackoverflow.com/questions/2626835/… Commented May 13, 2016 at 22:11

2 Answers 2

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)];
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

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.