I want to divide an array into 3 sub-arrays with random sizes.
For example the input array is I = [20, 5, 1, 2, 5, 10, 2, 5]
The sub-arrays could be like:
A = [20, 5]
B = [2, 5, 10]
C = [1, 2, 5]
I want to divide an array into 3 sub-arrays with random sizes.
For example the input array is I = [20, 5, 1, 2, 5, 10, 2, 5]
The sub-arrays could be like:
A = [20, 5]
B = [2, 5, 10]
C = [1, 2, 5]
By the examples you give it looks as though the sub-arrays can overlap so the number of sub-arrays you generate is not relevant. You just want a copy of the array between a random starting and ending position.
int[] getRandomSubArray(int[] array) {
Random random = new Random();
int start = random.nextInt(array.length);
int end = start + random.nextInt(array.length - start);
return Arrays.copyOfRandom(array, start, end);
}
Then you can generate as many as you want
int[] a = getRandomSubArray(array);
int[] b = getRandomSubArray(array);
int[] c = getRandomSubArray(array);