Randomization while displaying brand Images

Dear Support Team,

We have a requirement where in we need to show the respondent a list of brands with “Yes / No” as the options.

Also, the list of brands should not be shown all together but shown on separate screens with a max of 6 brands per screen.

Currently, using an Iterations loop with a single choice grid, having brands as topics and Yes/No as answers, along with the below function we have successfully implemented the above requirement.

Vars["AwCnt"] = 1 (only for the first iteration)


function FnVisibleCodes(CurrentQ,CheckQ,No)
    var k = 1;
    do {
      k = k + 1
    } while(k<=GetTopicCount(CurrentQ))
    var i,j;
    i = 1;
    j = Vars["AwCnt"];
       i = i + 1
     j = j + 1
    Vars["AwCnt"] = j
  }while(i<=No && j<=GetTopicCount(CurrentQ))

The problem we are now facing is that the brand list should be randomized and any 6 brands should be shown on the first screen, followed by another group of 6 brands that should be shown on the next screen and so on.

As can be seen in the above function, the first 6 brands are also shown in the first screen.

Is it possible for us to use Arrays to store brands and then randomize the array? If yes, kindly do send us an example on how to do the same.

Also, if there is any better logic for the above requirement, kindly do let us know.

Thanks and Regards,





Hi Deborah,

Yes, you can do as you wrote below. Here are the steps to do it:

Step one: add this code to the Survey Init code:

Vars["MaxTopicsShown"] = 6;
if (Vars["RandomTopics"] == "") {
Vars["RandomTopics"] = IntArrayToString(CreateRandomArray(GetTopicCount(XXX),1,GetTopicCount(XXX)));
What this code does is set the maximum topics to show per iteration to 6, then creates a random array in the size of the number of brands (which are the topics in question XXX, replace with your own question index)

Step two: is to add this code to the start script of the question in the iteration with the brands:

var randomTopics = StringToIntArray(Vars["RandomTopics"]);
for (var i=0;i<Vars["MaxTopicsShown"];i++) {
var offset = (IterationIndex-1) * Vars["MaxTopicsShown"];
var realTopicIdx = i+offset;

This code retrieves the random array we created earlier and then first sets all the topics to hidden (note we use one function for this instead of the loop you used as it is easier this way. Then, the code uses the iterationIndex and the maxtopicsshown configuration set earlier to calculate where we are in the random array and finally sets the relevant topic to false. Note it uses the random array as a pointer array to the real topics by using randomTopics[realTopicIdx] to get tot he real topic index out of the random array.


Step 3: add the following code to the entrance rule of the Chapter iteration (loop). First make sure to check the box Use entrance rule :

((IterationIndex-1) * Vars["MaxTopicsShown"]) < GetTopicCount(XXX)
This code will make sure that even if you configure by mistake more iterations that is necessary, the survey will only enter the iterations relevant for the number of topics (brands) and the max per page.

Important Notes:

Note in the init script we only initialize the randomTopics vars if it wasn't initialized before. This is to prevent the survey from generating a new random order if the survey is stopped and started later.
Note the use of the functions StringToIntArray() and IntArrayToString() which are used to store int arrays in VARS variables as they only accept string or int types. These functions take an array of ints and return the string representation of that array so that it can be stored in the VARS and then later you can convert it back to an int array using the StringToIntArray function.

Please let me know if you have any questions or need any assistance,




Hi Ofer,

Thanks again for helping us with our requirement.

We have used the code, along with a few modifications, and shown a randomized list of brands selected from a previous question.

Thanks and Regards,





Please sign in to leave a comment.

Didn't find what you were looking for?

New post