Create a questions with random text | XM Community
Question

Create a questions with random text


Imagine that you have a long list of words and want to ask the following question: What do you think of <RandomWord>?
One way you could do this is to have 10,000 different questions each with the text "What do you think of Apple" "What do you think of Cat?" etc...

However, then you would get back a data file that had 10,000 null fields and just one field with an answer. Worse yet, if you asked for timeing you would get back 50,000 + null fields and Qualtrics will not export that many fields.

Another, much better way to do it, would be to generate the question on the fly and then just have one question that had different text each time it was asked.

The first is the default in Qualtrics, and there does not seem to be a way to do the second. Does anyone have an idea on how to do the second choice.

16 replies

Userlevel 5
Badge +6
Hi @Nightblade,
you can use JavaScript to get this.

Here is an example with 2 words:
1) Put following HTML element with an id to the place where should be your random text:
`<div id="random">.</div>`

2) Put following JavaScript to your question:

Qualtrics.SurveyEngine.addOnReady(function() {
var words = ["cat", "dog", "horse"]; //Your words
var i = Math.floor(Math.random() * words.length); //randomly select an index
var my_word = words[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'word', my_word); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
document.getElementById("random").innerHTML = my_word; //show the word to respondents
});

Note: Don't forget to define your embedded field in the survey flow
Userlevel 7
Badge +27
There are a number of different ways to do the second. One that requires minimal coding and isn't too onerous to set up:

1. Make your 10,000 words choices in a MC question.
2. Use advanced randomization to randomly display one word and evenly present
3. Add a page break after the question
3. Use JS to either hide the question or click the next button
4. Pipe the displayed choices from your hidden question into your question text

If it were me, I would put the 10,000 words in an external database and write a web service script to randomly grab one a return it as an embedded variable. Then I would pipe the embedded variable into the question text.
> @fleb said:
> Hi @Nightblade,
> you can use JavaScript to get this.
>
> Here is an example with 2 words:
> 1) Put following HTML element with an id to the place where should be your random text:
> `<div id="random">.</div>`
>
> 2) Put following JavaScript to your question:
>
> Qualtrics.SurveyEngine.addOnReady(function() {
> var words = ["cat", "dog", "horse"]; //Your words
> var i = Math.floor(Math.random() * words.length); //randomly select an index
> var my_word = words[i]; //select a word
> Qualtrics.SurveyEngine.setEmbeddedData( 'word', my_word); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
> document.getElementById("random").innerHTML = my_word; //show the word to respondents
> });
>
> Note: Don't forget to define your embedded field in the survey flow
>
>
Hello @fleb,

I appreciate your answer. Could you expand on how to define the embedded field in the survey flow or direct my attention to how this is done?
Userlevel 5
Badge +6
> @keharmon said:
> Hello @fleb,
> I appreciate your answer. Could you expand on how to define the embedded field in the survey flow or direct my attention to how this is done?

Look at this tutorial.
I have a follow up on the OP question. Say I want to ask respondents 80 questions of the form "What percentage of X do you think are Y?" For example, let's say X is a list of animals (dog, cat, hippo, horse, etc.) and Y is a list of characteristics (tall, red, blue, short, fat, etc.).

So question 1 might be "What percentage of dogs do you think are small?" Question two might be "What percentage of cats do you think are blue?" and so on until we ask each person 80 of these with each combination of X and Y being random.

I would like the data to come out in the form of 2 columns for each question. So, column 1 for Question 1 would say which two characteristics the respondent was presented (e.g., dog, small) and column 2 would say what the respondent answered (e.g., 60%-80%). The second question that each respondent saw would then have the same two columns (the combination they saw and what they answered). So, if I ask each person 80 questions, I would end up with 160 columns with one row for each respondent.

Does anyone know how I could do this?
Userlevel 5
Badge +6
> @keharmon said:
> I have a follow up on the OP question. Say I want to ask respondents 80 questions of the form "What percentage of X do you think are Y?" For example, let's say X is a list of animals (dog, cat, hippo, horse, etc.) and Y is a list of characteristics (tall, red, blue, short, fat, etc.).
>
> So question 1 might be "What percentage of dogs do you think are small?" Question two might be "What percentage of cats do you think are blue?" and so on until we ask each person 80 of these with each combination of X and Y being random.
>
> I would like the data to come out in the form of 2 columns for each question. So, column 1 for Question 1 would say which two characteristics the respondent was presented (e.g., dog, small) and column 2 would say what the respondent answered (e.g., 60%-80%). The second question that each respondent saw would then have the same two columns (the combination they saw and what they answered). So, if I ask each person 80 questions, I would end up with 160 columns with one row for each respondent.
>
> Does anyone know how I could do this?

Hi @keharmon,
I think I have a good message for you. You can ask your respondents these 80 questions quite easily without even using Java script. Unfortunately I don't see any way how to get the output in the form you describe. You would have to have a separate table for each respondent and I don't think it is possible.

You can do following:
1) Create an excel/csv file with 80 rows and 3 columns with all your combinations. In the first columns there should be both your words (e.g.: "tall_dog"; it is used to generate column names), in the second just X and in the last just Y. (You can use for example R or bash if you don't want to do it manually).
2) Create a block with your question, turn on "Loop and merge". Insert there your tables. Randomize loop order.
3) Use piped text to show respondents you words at appropriate places.
Hi @fleb

Thank you for your assistance. I've been able to get my questions to display the way I want using both the Loop and Merge technique and JavaScript methods.

However, I am having issues with storing the data. When I tried using Javascript, the csv file I downloaded from Qualtrics did not contain the X and Y that the respondent was show. That is, it displayed their response to the question but not which combination of X and Y they were asked.

When I use Loop and merge with piped text, another issue arises. Because I have 80 variations of X and 80 variations of Y, there are 6400 combinations that a respondent can be asked. So, if I want to keep collecting data until I have asked every combination, I will end up with 6400 columns. Do you know if there are any features that allow for storing the data differently, or am I stuck with 6400 columns?
Userlevel 5
Badge +6
Hi @keharmon,
I thought that you have 80 combinations not 80 Xs and Ys. So you have 80 Xs, 80 Ys and you want to ask each respondent random 80 combination?
In this case, I think that JavaScript should be better for you. You wrote that
> When I tried using Javascript, the csv file I downloaded from Qualtrics did not contain the X and Y that the respondent was show.
Do you have your embedded field defined in the survey flow manually? If not, your data can't be stored.
I think you might could use Loop & Merge together with JavaScript. Like this you could have just two columns. Your script would execute at each loop. You could get the actual value of two embedded fields and you could always get the actual value (using piped text or `Qualtrics.SurveyEngine.getEmbeddedData`), concatenate in with the new value and send it back to the embedded data.
Thankyou @fleb

Though we have defined embedded field in the survey flow. We still can't save the word to the csv file. For example !

and our code is we have two embedded fields as:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
/*var order = "${e://Field/random1}"
Qualtrics.SurveyEngine.setEmbeddedData('random1', order);
document.getElementById("random1").innerHTML = order;*/
var words = ["cat", "dog", "horse"]; //Your words
var i = Math.floor(Math.random() * words.length); //randomly select an index
var my_word = words[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'words', my_word); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
//Qualtrics.SurveyEngine.getEmbeddedData('words')
document.getElementById("random1").innerHTML = my_word; //show the word to respondents

var words1 = ["red", "blue", "green"]; //Your words
var i = Math.floor(Math.random() * words1.length); //randomly select an index
var my_word1 = words1[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'words1', my_word1); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
document.getElementById("random2").innerHTML = my_word1; //show the word to respondents


});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

and the HTML looks like
what percentage of <div id="random1">.</div> do you think are <div id="random2">.</div>

If we use piped text of embedded data field, it is not getting displayed to the respondents. Adding loop and merge produces 6400 columns. Can you help me storing this embedded data fields?
Userlevel 5
Badge +6
Hi @keharmon,
firstly, you have wrong notation for your comments in the top of your script. Therefore your script crashes and nothing happens regardless whether your embedded fields are defined or not. When I removed the first four lines, the script worked.
Haven't you make a mistake in copying your HTML? There are no elements where to put your texts. It should look like this: `What percentage of <span id = "random1"></span> do you think are <span id = "random2"></span>?`
I have no idea how could you achieve 6400 columns. You should have 80 columns with responses and two with your embedded data. Alternatively, you could have your responses also in one column separated by some separator. Your 80 columns will stay there,but you don't need t use them.

Qualtrics.SurveyEngine.addOnReady(function()
{
var words = ["cat", "dog", "horse"]; //Your words
var i = Math.floor(Math.random() * words.length); //randomly select an index
var my_word = words[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'words', "${e://Field/words}" + my_word + ";"); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
//Qualtrics.SurveyEngine.getEmbeddedData('words')
document.getElementById("random1").innerHTML = my_word; //show the word to respondents

var words1 = ["red", "blue", "green"]; //Your words
var i = Math.floor(Math.random() * words1.length); //randomly select an index
var my_word1 = words1[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'words1', "${e://Field/words1}" + my_word1 + ";"); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
document.getElementById("random2").innerHTML = my_word1; //show the word to respondents

document.getElementById('NextButton').onclick = function()
{
var currentQuestionID = this.getQuestionInfo().QuestionID
var questionObject = Qualtrics.SurveyEngine.getInstance(currentQuestionID);
var currentResponse = questionObject.getSelectedChoices()[0];
var choicesObject=questionObject.getQuestionInfo().Choices;
var currentChoiceText=choicesObject[currentResponse].Text;
Qualtrics.SurveyEngine.setEmbeddedData( 'answers', "${e://Field/answers}" + currentChoiceText + ";");

});
Thanks @fleb

The issue of storing is still a problem. Though it randomly chooses word from 2 lists and display them, when coming to storage it always stores the same responses no matter what X and Y are displayed.

!


This is our embedded fields in survey flow

!

It is choosing the Last word from the list while saving back to the QUALTRICS. Is there a possible way to store the randomly displayed word to Qualtrics?
Userlevel 5
Badge +6
Hi @keharmon,
I've tried my script once again. There is a problem with recording your responses in the same way as words, but the first part of the script works. Why do you have there that many embedded fields? You need just 2. Are they defined in the very beginning of the survey flow?
Thanks! @fleb
Can we store our embedded data dynamically into loop and merge fields?
What I am trying is to insert specific words into shuffled version of X and shuffled version of Y for example If X=["a","b","c","d","e"]
shuffled X = ["b","e","a","d","c"]
I want to insert p and q and form new array New X =["b","e","p","a","d","q","c"] and the same with Y.

Now I want first element of New X and first element of New Y in my first question and respective second words of X and Y in second question and so on...
instead of using random index , is there any other way to display questions ad mentioned above?
Hi @TomG , I have been following your comments from various posts and they have been super helpful. Thank you so much for your contribution!

I am using your method to construct just 1 question that contains pipe text that present 1 random thing from a set of 40 things.

Following your suggestion in this post, would checking 'evenly present elements' mean all 40 things will be presented equally across participants?

My intention is to just randomly show 1 out of 40 things for each participant, and all 40 things are not necessarily presented. Do I need to check the option?
Userlevel 7
Badge +27
> @Sunsiree said:
> Hi @TomG , I have been following your comments from various posts and they have been super helpful. Thank you so much for your contribution!
>
> I am using your method to construct just 1 question that contains pipe text that present 1 random thing from a set of 40 things.
>
> Following your suggestion in this post, would checking 'evenly present elements' mean all 40 things will be presented equally across participants?
>
> My intention is to just randomly show 1 out of 40 things for each participant, and all 40 things are not necessarily presented. Do I need to check the option?
>
>
Checking 'evenly present elements' keeps the distribution of items even across respondents. So, if you have 40 respondents each respondent will see a different item. If you have 160 respondents each item will be displayed 4 times. If you don't check evenly present the distribution will be random, so out of 160 respondents item 1 might be displayed 8 times, item 2 might be displayed 1 time, etc.
@TomG Thank you so much for your answer! Appreciate your comments and contribution to this Qualtrics community platform.

Leave a Reply