Let’s say I have a list of ratios in percents.
Map<Integer, Integer> ratio = new HashMap<Integer, Integer>();
ratio.put(0, 10);
ratio.put(1, 50);
ratio.put(3, 40);
Then I have a two dimensional array.
int[][] arr = new int[50][50];
Now I want to randomly, but evenly, distribute the defined values 0, 1 and 3 with their respective ratio of 10, 50 and 40 in that array.
How do I do that?
Fill an 1D array with first zeros, second 1 and then 3.
Something like:
Than take a Shuffle algorithm and suffle your 1D array. Finally fill up your 2D array.
Since your 2D array have 2500 position (50^2), in the finally you should have 250 ‘0’ (0.10 * 50^2), 1250 ‘1’ (0.50 * 50^2) and 1000 ‘3’ (0.4 * 50^2).