Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Randomize categories and answers within each category anchoring ‘other’ options.

I have a select question with answer 5 categories. Then there are multiple answer options within each category.  I need to randomize the answer options within each category, and then randomize the order the categories are displayed.

I would like to be able to do this without creating 5 questions on the page [randomizing the options in each - and then randomizing the questions on the page]

I there a way to do this with constructed lists?

Thanks!
Julia
asked Aug 6, 2017 by jas8278 Bronze (750 points)

1 Answer

0 votes
You are correct. Constructed lists can solve your problem.

Let's assume you have 5 categories and 3 codes per category.

Create 1 parent list (call it CategoryList) that represents all codes for all categories. So in total, you have 15 codes in this parent list.

Create 5 constructed lists where each constructed list represents each category.  See below ...

Category1ConList
ADD(CategoryList,1,3)
Randomize()

Category2ConList
ADD(CategoryList,4,6)
Randomize()

Category3ConList
ADD(CategoryList,7,9)
Randomize()

Category4ConList
ADD(CategoryList,10,12)
Randomize()

Category5ConList
ADD(CategoryList,13,15)
Randomize()

Each of the above constructed lists will randomise codes within each category.

Now create a parent list called OrderList. It will contain 5 codes: 1-2-3-4-5.

Create a constructed list called OrderConList which will use OrderList as the parent list ...
ADD(OrderList)
Randomize()

This last constructed list (call it RandCategoryConList) will bring all categories together and then also randomise all categories ...
Begin Unverified Perl
 
 ADD("Category".LISTVALUE("OrderConList",1)."ConList",1);
 ADD("Category".LISTVALUE("OrderConList",2)."ConList",1);
 ADD("Category".LISTVALUE("OrderConList",3)."ConList",1);
 ADD("Category".LISTVALUE("OrderConList",4)."ConList",1);
 ADD("Category".LISTVALUE("OrderConList",5)."ConList",1);
  
End Unverified 

Use RandCategoryConList as the list to display at your specific question.

If you have any generic codes like Other None, Don't know, etc., simply add them to your CategoryList parent list as codes 16-17-18-etc. And in the
RandCategoryConList at the bottom, pop in something like the example below ...
ADD("CategoryList",16,18);

Good luck.
answered Aug 6, 2017 by Paul Moon Platinum (62,380 points)
Thanks Paul - just one problem, I'm getting the following error message:

Error: List 'RandCategoryConList' - List Instructions - '.' is an invalid character.
Apologies.

I didn't clean up my copy and paste entirely.

Try this script for RandCategoryConList ...
Begin Unverified Perl
  
 ADD("Category".LISTVALUE("OrderConList",1)."ConList");
 ADD("Category".LISTVALUE("OrderConList",2)."ConList");
 ADD("Category".LISTVALUE("OrderConList",3)."ConList");
 ADD("Category".LISTVALUE("OrderConList",4)."ConList");
 ADD("Category".LISTVALUE("OrderConList",5)."ConList");
   
End Unverified 
No worries . . . thanks so much for your help!
That's working great . . . now my life just more difficult.  Client wants a header at the beginning of each category.  I usually use this code to do headers:
<script type="text/javascript">
$(document).ready(function(){
    $('#S4_div > .question_body .response_column > div:nth-child(2)')
        .before('<div><br><b>Smartphones</b></div>');
})
</script>


for each category header.  But because of the category randomization, I don't know what number the "div:nth-child(2)" will be.

Is there a way to assign the category header to show before it's list of options when they are randomized?
Zachary Anderson at Sawtooth Software has published a wonderful question library on their website. It contains a whole bunch of handy advanced question types and features.

Check out: http://www.sawtoothsoftware.com/community-question-library/1770-subheaders.

I have used this before. It works great. Make sure you follow the instructions he has put in place.
Dang!  that is some sweet code - - hats off to Zachary.  Thanks Paul!
Zachary is a legend!!! A very generous fellow who has some valuable skills. He's very accommodating.
Instead of creating a dummy parent list and constructed list in order to randomize the blocks, you can try the below piece of code in final constructed list

Begin Unverified Perl
use List::Util qw(shuffle);

my @sorted_array = (1,2,3,4); #add number of elements based on number of lists
my @random_array = shuffle(@sorted_array);

foreach $a (@random_array)
{
    ADD("Category".$a."ConList");
}
End Unverified
...