I have this JavaScript which displays x random numbers of which sum is a given number y. (120 splitted to 5 random parts currently)
I want to make it display all possible combinations of number like a list, but allow only numbers from 1-50.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
public class Demo {
public static Random randNum = new Random();
public static int[] DemoFunc(int number, int parts) {
HashSet<Integer>set = new HashSet<Integer>();
set.add(0);
set.add(0);
set.add(0);
set.add(number);
int arrSize = parts + 1;
while (set.size() < arrSize) {
set.add(1 + randNum.nextInt(number - 1));
}
Integer[] dividers = set.toArray(new Integer[arrSize]);
Arrays.sort(dividers);
int[] res = new int[parts];
for(int i = 1, j = 0; i<dividers.length; ++i, ++j) {
res[j] = dividers[i] - dividers[j];
}
return res;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(DemoFunc(120, 5)));
}
}
Aucun commentaire:
Enregistrer un commentaire