Randomly choose an item from an external list | XM Community
Skip to main content
Hi all,

I am trying to do the following and don't seem to find a solution for it:

1. I want to draw 250 random integers between 50 and 800

2. these 250 integers will be used throughout my study, more specifically I need to draw randomly from these 250 integers repeatedly



The first step seems to be easy with the "random number" generator of the embedded data. However, I can't use this list then to again randomly draw from it. So my idea was to use an imported a list with the 250 randomly drawn integers and within Qualtrics randomly draw from them. But that doesn't seem to be possible.



Using the randomizer function would potentially be a solution but then I'd have to write 250 fields under this randomizer - which doesn't seem like the smoothest solution.



Any ideas?
Generally for such setup external web services are created to fetch random number in a desired pattern. This web service is then called using qualtrics survey flow web service elements.
You could do both steps with JavaScript.



Generate 250 random integers between 50 and 800 and save as embedded data:

```

Qualtrics.SurveyEngine.addOnload(function() {

function randomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

var randInts = [];

for(i=0;i<250;i++) { randInts.push(randomInteger(50,800)); }

Qualtrics.SurveyEngine.setEmbeddedData("randInts", randInts.join(","));

});

```

Randomly draw a single integer from randInts and save as embedded data:

```

Qualtrics.SurveyEngine.addOnload(function() {

var randInts = "${e://Field/randInts}".split(",");

Qualtrics.SurveyEngine.setEmbeddedData("randInt", randInts[Math.floor(Math.random() * randInts.length)]);

})

```
Thank you both for your helpful comments! I feel like that TomG's solution is a bit smoother. Where would I insert the JavaScript? At the first item of my survey?
I have tried to paste the code into the "Footer"of the "Look & Feel"section to make the variables accessible on a global scale. However when trying to call on the variable "randInt1" using piped text nothing happens. Any ideas?



Here's the code I used:

`<script type="text/javascript">

Qualtrics.SurveyEngine.addOnload(function() {

function randomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

var randInts = ];

for(i=0;i<250;i++) { randInts.push(randomInteger(50,800)); }

Qualtrics.SurveyEngine.setEmbeddedData("randInts", randInts.join(","));

});



Qualtrics.SurveyEngine.addOnload(function() {

var randInts = "${e://Field/randInts}".split(",");

Qualtrics.SurveyEngine.setEmbeddedData("randInt1", randIntssMath.floor(Math.random() * randInts.length)]);

});

</script>`
> @LifetimeStudent said:

> I have tried to paste the code into the "Footer"of the "Look & Feel"section to make the variables accessible on a global scale. However when trying to call on the variable "randInt1" using piped text nothing happens. Any ideas?

>

> Here's the code I used:

> `<script type="text/javascript">

> Qualtrics.SurveyEngine.addOnload(function() {

> function randomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

> var randInts = ];

> for(i=0;i<250;i++) { randInts.push(randomInteger(50,800)); }

> Qualtrics.SurveyEngine.setEmbeddedData("randInts", randInts.join(","));

> });

>

> Qualtrics.SurveyEngine.addOnload(function() {

> var randInts = "${e://Field/randInts}".split(",");

> Qualtrics.SurveyEngine.setEmbeddedData("randInt1", randIntssMath.floor(Math.random() * randInts.length)]);

> });

> </script>`



Add the first script to the first question in your survey. It only needs to be executed once. Once you go to the next page, the embedded variable randInts will be populated (you can access it using ${e://Field/randInts})



Add the second script to a question any time you need to draw a different random number from the list. It is saving the random number to a embedded variable, so do it on the page before you need to pipe it. If you need to do it on the same page, you could use JavaScript to draw the number then update the html content of a wrapper element on the page.
Thanks - I think I tried to call the piped text too early! Seems to work!

Leave a Reply