Script to assign random group to a respondent


After my respondent passes through the screener I’d like to randomly assign them to a group. Group 1, 2 or 3. Later in the survey those groups will determine whether or not they see a particular question.  1/3 of the respondents need to see a question while 2/3 need to skip past it.  

In Excel I can use RandBetween(1,3).  Is there a script that will allow me to do the same thing in SG and capture the assigned number in a hidden value which I can use in logic later in the survey? 

I’m a beginner at script writing but think this might be easy for someone with experience.  Please share! 

amy_aha answered

    Oh, I totally overlooked that feature of the hidden value action. But you’re right that I need to guarantee that I can get a third of respondents into Group 1. 

    There was another idea SG staffer Brianne gave me related to A/B branching that I need to look more into as well, but my fallback plan is to create two links with different URL variables and recruit 1/3 of my total to one link. Then I’ll use logic based on the URL variables. 

    It sounds like that might need to be my game plan until I can investigate these other ideas further. 

    amy_aha answered

      I don’t think this will require a script. You can use the Hidden Value Action itself to generate a random number between 1-3 and then base logic conditions on the generated number later in your survey.

      Take a look at this section of the Hidden Value article:

      One of the ‘calculated value options’ is ‘a randomly generated number’.

      One draw back here is that this is truly random, so would not guarantee that 1/3 of the respondents would see your question. Rather, all respondents would have a 1 in 3 chance to see the question.

      Not sure if that’s a deal breaker…

      Dominic Sharpe answered


        Question stats

        • Active
        • Views6606 times
        • Answers2 answers
        • Followers1 follower