Inserting a random number from specified distribution into survey question | XM Community
Question

Inserting a random number from specified distribution into survey question

  • 8 April 2024
  • 3 replies
  • 23 views

Badge +1

In R-studio, I have generated a numeric distribution (n = 1000) with specific parameters. In my survey, I would like to randomly select a single number from this distribution as part of a decision task for participants. So far, I have created an Embedded Data field in Survey Flow by copy-pasting in my distribution of numbers (separated by comma’s). However, I am unable to use piped text to select a single random number from this distribution to be displayed in my survey text. I would be grateful to receive some advice on how to approach this matter.


3 replies

Userlevel 5
Badge +12

Hi @George Otherfoot Can you elaborate more on the part you mentioned about “I would like to randomly select a single number from this distribution as part of a decision task for participants.”

Meanwhile, you can try using ‘Random number’ element present in the survey flow to generate a random number and then pipe text in the survey.

Pipe text the embedded data ‘Random_number_selected’ to display it in the survey flow.

 

Badge +1

I have come up with a solution to randomly draw from field A1:

<script>
Qualtrics.SurveyEngine.addOnload(function() {
    var numberArray = "${e://Field/A1}".split(',');
    var randomIndex = Math.floor(Math.random() * numberArray.length);
    var selectedNumber = numberArray[randomIndex].trim();
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedNumber", selectedNumber);
    document.querySelector('.QuestionText').innerHTML = "The randomly selected number is: " + selectedNumber;
});
</script>

Badge +1

Thank you @omkarkewat for your quick reponse. Sorry for not being entirely clear in my question.

 

The random number element was not suitable for me because the distribution I would like to draw from has specific characteristics (i.e. not an equal chance for every number in a certain range). I have made a javascript code that I have posted that seems to work. Perhaps it will be useful for someone searching for a similar solution in the future.

 

Sorry for taking your time and again thanks for your response. 

Leave a Reply