samedi 16 avril 2016

Java random distribution

I have an array of Objects which is [36] long and have 4 different arrays. The Objects each have their owner and the number of owners depends on user input. The 0 means the object doesn't have an owner and 1, 2, 3, 4, 5 is the identification of different owners.

The goal is to randomly disribute the Objects between these owners. In other words if the user wants 4 owner (case 3) i have to randomly set 4 object without an owner and 8 object for each owner.

public int[][] shuffle = {{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                          {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3},
                          {0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4},
                          {0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5}};

At first i tried to copy the needed row from the array and shuffle it up with some kind of Fisher - Yates algorithm then set the Objects owner but i couldn't do it.




Aucun commentaire:

Enregistrer un commentaire