views:

70

answers:

4

I have a program with four different buttons. I want to interchange the position of the buttons randomly. For example: 1 2 3 4 Later: 3 4 1 2 Later: 1 3 2 4

Is there a algorithms for that? The only way I can think is to make a random number from 1 to 24 (24 possibilities) and then code all the possible button postitions.

     int foo = arcrandom() % 23;
      switch(foo){
       case 0:
        button1postiton = 100; //just an example
        button2position = 200;
        button3position = 300;
        button4position = 400;
        break;
       case 2:
        button1postiton = 200;
        //blablabla and so on and so on
}

But is there a more efficient way?

Thanks!

+5  A: 

You could shuffle the buttons or their positions, e.g. with a Fisher-Yates shuffle.

Svante
Thanks for the answer! Can you explain, how the Fisher-Yates shuffle works? I never worked with it.
Flocked
I don't want to sound condescending, but have you thought about passing "fisher yates shuffle" to your favourite search engine? The basic idea is to pick one of the remaining elements at random at each step.
Svante
I did it and found the answer - Look to my own answer ;)
Flocked
+1  A: 

There is code in this website to get a list of all permutations of an array (see method perm2), it is coded for char arrays, but can be modified to do int arrays as well and to other languages as well, then you can use mjv's idea.

http://www.cs.princeton.edu/introcs/23recursion/Permutations.java.html

If in Java, this is what I would try....

Once you get all the possible permutations maybe in a vector, I think you can use a grid bag layout and change the grid constraints, picking one of the elements of the vector randomly. I have not tried this out, but I am thinking along the lines of

Vector permutations = ... //get the permutation using a class similar to the one in the website for an array of ints {0,1,2,3}

//The panel
JPanel  pane;
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//Choose one permutation at random
int foo = arcrandom() % 23;
int current[] = permutations.get(foo);

//Add the buttons in the chosen order
button = new JButton("Button 1");
c.gridx = current[0];
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 2");
c.gridx = current[1];
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 3");
c.gridx = current[2];
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 4");
c.gridx = current[3];
c.gridy = 0;
pane.add(button, c);

Let me know if this works!

Anu
+1  A: 

Start with a random number 0 <= r < 24

Start with your first position. Derive rr = r % 4 and r = r / 4. Those are the remainder and quotient respectively after division by 4.

The remainder specifies a position. Swap position 0 with the specified position.

For the next position, derive rr = r % 3 and r = r / 3. Again the remainder specifies a position, this time 0, 1 or 2, but relative to your current position (1).

Swap position 1 with position rr+1.

For the next position, derive rr = r % 2 and r = r / 2. Again the remainder specifies a position, this time 0 or 1, and relative to your current position again (2).

Swap position 2 with position rr+2.

For position 3, there is nothing to do.

Note - for each swap, one possibility is to swap a position with itself. Obviously no swap is needed for that.

This is probably the Fisher-Yates shuffle - I had no idea it had a name until today.

Steve314
It is analogous to Fisher-Yates, but it's usually explained as "start with element 0, exchange that with a random element between 0 and the end; next element 1, replace that with a random element between 1 and the end; next element 2..."
Vatine
OK - makes sense - with a small number of cases working from a single random number makes sense, but obviously this breaks down when e.g. shuffling a deck of cards due to the size of the random number needed, so what you describe is clearly the more general solution.
Steve314
A: 

Thanks for all your answers! I used the Fisher-Yates shuffle! I found here a nice tutorial, how to use the algorithm in Objective-C: gorbster.net

Flocked