VI. Random Group and Role Assignment

Getting Started:

In order to randomly assign Group or Role IDs programmatically,  an algorithm must be used to shuffle a sequence of the desired IDs to be assigned to each subject.

A derivative of the Fisher-Yates shuffle algorithm used to perform the random shuffling of IDs. It is important to point out that this algorithm is extremely useful in cases where you would like to randomly assign each elelement of a set to a subject. The set of values in this set could be IDs, Payoffs, Probabilities, etc.

For the purposes of expedience, the Analogy explaining the functionality of the algorithm can be skipped. Yet,  I believe it will benefit you to understand how the algorithm works should you need to modify it.

 

Shuffle Algorithm: A Simple Analogy

An experimenter has 5 subjects. Each must be assigned a random ID. The experimenter, who at the moment does not have access to Z-Tree, takes 5 index cards and numbers them (1-5). The experimenter takes out 5 bins, labels each bin 1 through 5, groups them together, and names them the “Temporary Set”. The experimenter takes out another 5 bins, labels each bin as before, groups them together, and names them the “Shuffled Set”.

The experimenter then says to the subjects, “I assign each bin in the Shuffled Set to one of you. When I am done performing the following steps a card will be placed in your bin with your corresponding (random) ID.”

Now that all of the preparations have been made the procedure (Algorithm) begins…

  1. Bin 2 is randomly selected.That is, 2 was the random integer selected between the first bin index (1), and the index of  the last bincontaining a card (5, in this case).
  2. The card in the randomly selected bin is moved from the Temporary Set to the first empty bin in Shuffled Set (1).
  3. The cards in to the right of the randomly selected bin (2) are shifted left.
  4.  Bin 3 is randomly selected.That is, 3 was the random integer selected between the first index (1) and the last used bin containinga card (4, in this case).
  5. The card in the randomly selected bin is moved to the first free bin in the Shuffled Set (2).
  6. The cards to the right of the randomly selected bin (3) are shifted to the left.
  7. Bin 3 is randomly selected.That is, 3 was the random integer selected between the first index (1) and the last used bin containinga card (3, in this case).
  8. The card in the randomly selected bin is moved  to the first free bin in the Shuffled Set (3).
  9. No card are to the right of the randomly selected bin (3), so none are shifted left.
  10. Bin 1 is randomly selected.That is, 1 was the random integer selected between the first index (1) and the last used bin containinga card (2, in this case).
  11. The card in the randomly selected bin is moved to the first free bin the in the Shuffled Set (4).
  12. The cards in the bins to the right of the randomly select bin are shifted left.
  13. Bin 1 is randomly selected.That is, 1 was the random integer selected between the first index (1) and the last used bin containinga card (1, in this case).
  14. The card in the randomly selected bin is moved to the first free bin in the Shuffled Set (5).
  15. There are no cards in the bins to the right of the randomly selected bin, so no cards are shifted left.

Now each Subject has been assigned a random ID.

Random Subject ID:

To use the algorithm provided blow in order to assign random Subject IDs place Module A, B, and C in a subjects
table program. The arrayMax value in Module A must bet set to the number of subjects in the experiment. Create
a Subjects program immediately following the algorithm and enter the following:

SubjectID = shuffle[Subject];

Now use SubjectID as the randomly assigned Subject ID throughout the experiment.

Random Role ID:

To use the algorithm provided below for random Role assignment Module B must be replaced with module B’ provided
below. Note the value for maxRoleID should be the maximum role ID assigned. For example, if you would like to assign
Role IDs 1 and 2, the maxRoleID would be 2; If you wanted to assign Role IDs 1, 2, and 3 the maxRoleID would be 3 etc.

Module B’

i = 1;
maxRoleID = 2;
while (i <= arrayMax) {
    tempArray[i] = mod(i,maxRoleID)+1;
    i = i + 1;
}

Random Group ID:

To use the algorithm provided below for randome Group ID assignment Module B must be replaced with module B’ provided
below. The value of maxGroupID should be changed to the largest integer in the set of group IDs. For example if there are 3
groups the maxGroupID would be 3.

Module B’

i = 1;
maxGroupID = 2;
while (i <= arrayMax) {
    tempArray[i] = mod(i,maxGroupID)+1;
    i = i + 1;
}

 

The Generic Algorithm:

The algorithm below (Module A, B, and C) should be placed inside a Subjects Table program. If module C is included
It will place a sequence of integers from o to arrayMax (variable below) in the temporary array, shuffle them, and place
the result in the shuffle array.

Each subject can access their shuffled value by using their Subject id as an index to the shuffle array.

shuffle[Subject];

Note: The arrayMax variable in Module A contains the size of the of the array (number of bins). This
value must be modified to suite your needs.

Module A

// arrayMax Contains the size of the 
arrayMax = 6;
array shuffle[arrayMax];
array tempArray[arrayMax];

 

Note: this portion of the algorithm should only be used if you want to have a sequence of integers shuffled.
If for example you would like to randomly assign role IDs 1 and 2 randomly to each subject you would have to
substitute module B with a module that places an equal number of ones and twos for each subject. See the
Random Role ID section above.

Module B

i = 1;
// Places values [1, 2, 3, 4, 5, 6] in tempArray
while (i <= arrayMax) {
    tempArray[i] = i;
    i = i + 1;
}

Note: module C performs the algorithm outlined above analogy.

Module C

n = 1;
end = arrayMax;

while (n <= arrayMax) {
    rand = rounddown(end*random(), 1) + 1;
    shuffle[n] = tempArray[rand];

    // shift each element to the right from rand + 1 till the end
    if (rand < arrayMax) {
        k = rand + 1;
        while (k <= end) {
            tempArray[k-1] = tempArray[k];
            k = k + 1;
        }
    }
    end = end - 1;
    n = n + 1;
}