0

I have a java project done for school. This is a piece of code which i am having a hard time to understand its logic. Please shed some light on it.

for(int i = 0; i< leftbut.length; i++){
          int randomNumber =(int)(Math.random()*leftbut.length);
             tempNum = leftbut[randomNumber];
            leftbut[randomNumber] = leftbut[i];
            leftbut[i]=tempNum;      

     }

The leftbut in this case is actually an array of 9 buttons. This code is supposed to shuffle the 9 buttons in different positions. I just cant understand how this code works.

KAKAK
  • 879
  • 7
  • 16
  • 32

3 Answers3

2

The code generate a random permutation of the original array.

However, note that this is biased - it does not generate all permutations in uniform distribution. This thread discusses what is the affect of this bias.

To overcome this issue - you might want to have a look on fisher yates shuffle (which main difference is, to generate a random number in range [i,n) in each iteration instead of in range [0,n).)


EDIT:
You might better understand it if you encapsulate the assignments in a method:

private static void swap(int[] array, int i, int j) { 
       tempNum = array[j];
       array[j] = array[i];
       array[i]=tempNum; 
}

Now, the code will be simpler to follow:

for(int i = 0; i< leftbut.length; i++) {
          //choose a random index in the array
          int randomNumber =(int)(Math.random()*leftbut.length);
          //swap the element in index i with the random element chosen in the array
          swap(leftbut, i, randomNumber);
}

The idea is you swap() each element in the array with a random index. This random index is chosen randomly from the array, and its index is denoted as randomNumber.
Since you only swap() items around, you can easily prove that the output array is a permutation of the original.

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • All the 9 buttons contain an ImageIcon. If this code randomly generates numbers which shuffles the JButton. There will be duplicate buttons with duplicate ImageIcon no? – KAKAK Feb 03 '13 at 12:56
  • @user2016977 A permutation contains no doubles, so if the elements in the original list are all unique, the result list will have all elements unique as well. – amit Feb 03 '13 at 12:58
  • What is a simple way to explain this? – KAKAK Feb 03 '13 at 13:04
  • Thank you Amit. I am working on it to understand. Thanks for the help – KAKAK Feb 03 '13 at 13:11
1

It just 9 times randomly swaps to elements of leftbut array.

Damian Jeżewski
  • 1,246
  • 2
  • 14
  • 21
  • All the 9 buttons contain an ImageIcon. If this code randomly generates numbers which shuffles the JButton. There will be duplicate buttons with duplicate ImageIcon no? – KAKAK Feb 03 '13 at 12:55
1
for(int i = 0; i< leftbut.length; i++){

Is a loop, it inizialize variable i to 0, and increment it each loop by 1

int randomNumber =(int)(Math.random()*leftbut.length);

declare the integer variable randomNumber and assign a random value in range 0 - length of array

 tempNum = leftbut[randomNumber];         
 leftbut[randomNumber] = leftbut[i];
 leftbut[i]=tempNum;  

this actually invert the 2 buttons position in the array, the value i become the random one and viceversa

lelloman
  • 13,883
  • 5
  • 63
  • 85