Have an idea?

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

Reverse segment of constructed list based on if logic

I am trying to randomly reverse the order of a response list.

I am able to successfully get the constructed list to display as I want it to using:
ADD(QGList,1,5)
    reverse()
    ADD(QGList,6,7)


But I'm struggling with adding the if logic. The code I'm trying isn't working and I'm not sure where to change it:
Begin Unverified Perl
if (VALUE("flipseed")==2){
    ADD("QGVOTERList",1,5);
    reverse();
    ADD("QGVOTERList",6,7);
} else{
    ADD("QGVOTERList");
}
End Unverified
   


The flipseed is declared in the footer of the intro screen using
[% SetValue(flipseed, RandNum(1,1,2)) %]
asked May 3 by julietcig (140 points)

1 Answer

0 votes
Your constructed list code looks fine, but there may be some confusion between the Sawtooth Script functions SysRand and RandNum.

Try running this:

SysRand(0, 9)
SysRand(0, 9)
SysRand(0, 9)
SysRand(0, 9)
SysRand(0, 9)


You may not get five different answers, but you will almost certainly not get the same answer repeated five times.  Calls to SysRand all independently return a "true" random number.

Now try running this:

RandNum(16, 0, 9)
RandNum(16, 0, 9)
RandNum(16, 0, 9)
RandNum(16, 0, 9)
RandNum(16, 0, 9)


I don't know what number that will return, but it will definitely return the same value all five times.  That's because RandNum is seeded.  Behind the scenes, RandNum does a bunch of crazy stuff with that "16" to decide which number between 0 and 9 it is going to return.  So long as we keep using the same seed with the min/max, we'll keep getting the same output.

You could go with whichever function you want, but if you are going to use RandNum, you'll want to use the respondent number of something similar as the seed so that not all respondents get the same response.
answered May 3 by Zachary Platinum Sawtooth Software, Inc. (114,950 points)
It turns out the issue was my reverse(); should have been REVERSE();

The rotate seed was fine
You do need to write Sawtooth Script functions in all caps in unverified Perl, but you will also need to apply one of my suggested changes above if you want the resulting constructed list to vary between respondents.  As-is, either all respondents will see the items in the 1-2-3-4-5-6-7 order or all respondents will see the items in the 5-4-3-2-1-6-7 order.
...