Reputation: 263
I want to shuffle my 2D array in java, the datas of this array:
2.5 2.6
2.0 3.0
3.0 3.0
And this is my shuffle method:
public void shuffleData(){
Random rnd = new Random();
double temp[][] = new double[this.NBaris][NKolom];
this.dataShuffle = new double[this.NBaris][NKolom];;
for(int i = this.NBaris-1; i >= 0; i--){
int index = rnd.nextInt(i + 1);
for(int j = 0; j < this.NKolom; j++){
temp[0][j] = this.data[index][j];
this.dataShuffle[index][j] = this.dataShuffle[i][j];
this.dataShuffle[i][j] = temp[0][j];
}
}
}
But the result is become like this when i print:
2.5 2.6
2.5 2.6
2.0 3.0
whats wrong with my method?, can you help me guys. thanks.
Upvotes: 0
Views: 1977
Reputation: 22983
There different problems
this.data[index][j]
(which might be multiple times the same value) and store it in array dataShuffle. This lead into unexpected duplicated rows, as you discover.If you only want to shuffle the order of the rows and leave the values always in the same column you could to it like this.
double[][] data = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
List<double[]> asList = Arrays.asList(data);
Collections.shuffle(asList);
data = asList.toArray(new double[0][0]);
System.out.println("toArray = " + Arrays.deepToString(data));
sample output
toArray = [[1.1, 1.2], [3.1, 3.2], [2.1, 2.2]]
If you not want to shuffle only the rows but also the values between the columns you could use this snippet
double[][] data = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
Random random = new Random();
int numberOfValues = data.length * data[0].length;
for (int i = numberOfValues - 1; i > 0; i--) {
int index = random.nextInt(i);
int row = i / data[0].length;
int column = i - row * data[0].length;
int randomRow = index / data[0].length;
int randomColumn = index - randomRow * data[0].length;
double temp = data[row][column];
data[row][column] = data[randomRow][randomColumn];
data[randomRow][randomColumn] = temp;
}
System.out.println("toArray = " + Arrays.deepToString(data));
sample output
toArray = [[2.1, 3.2], [2.2, 1.2], [1.1, 3.1]]
The idea is to treat all entries as a flat array [2.1, 3.2, 2.2, 1.2, 1.1, 3.1]
and to start from the last index back to the second.
0 <= index < 5
and swap the values on data[index] and data[5]0 <= index < 4
and swap the values on data[index] and data[4]Upvotes: 2