如何在Qualtrics中实现受前置刺激约束的字母数字无重复随机刺激呈现?
Hey there! Let's walk through how to build this setup where each participant sees exactly 3 stimuli—one for each letter (a/b/c) and one for each number (1/2/3)—with random order and no repeats. This is a classic balanced design problem, and we can pull it off with Embedded Data and a bit of JavaScript.
1. First, Map Out All Valid Stimulus Groups
First, let's clarify: the valid sets of stimuli are all the perfect one-to-one matches between letters and numbers. There are 6 total (since it's 3! permutations):
- Set 1: a1, b2, c3
- Set 2: a1, b3, c2
- Set 3: a2, b1, c3
- Set 4: a2, b3, c1
- Set 5: a3, b1, c2
- Set 6: a3, b2, c1
These are the only combinations that meet your rule (each letter/number appears exactly once per participant). We'll use these as our pool to randomize from.
2. Use Embedded Data to Store a Randomly Selected Set
Head over to your Survey Flow and add an Embedded Data element first. Name variables like StimulusSet, Stim1, Stim2, Stim3—these will hold the stimuli assigned to each participant.
Next, add a JavaScript element right after the Embedded Data block. This code will randomly pick one of the 6 valid sets, shuffle its order (if you want random presentation), and save it to the Embedded Data variables:
// Define all valid stimulus combinations const validSets = [ ["a1", "b2", "c3"], ["a1", "b3", "c2"], ["a2", "b1", "c3"], ["a2", "b3", "c1"], ["a3", "b1", "c2"], ["a3", "b2", "c1"] ]; // Pick a random set from the pool const randomSetIndex = Math.floor(Math.random() * validSets.length); let selectedSet = validSets[randomSetIndex]; // Shuffle the set for random presentation order (Fisher-Yates algorithm) function shuffleArray(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; } selectedSet = shuffleArray(selectedSet); // Save to Embedded Data so we can use it in questions Qualtrics.SurveyEngine.setEmbeddedData("StimulusSet", JSON.stringify(selectedSet)); Qualtrics.SurveyEngine.setEmbeddedData("Stim1", selectedSet[0]); Qualtrics.SurveyEngine.setEmbeddedData("Stim2", selectedSet[1]); Qualtrics.SurveyEngine.setEmbeddedData("Stim3", selectedSet[2]);
3. Insert Stimuli Into Your Questions
Create 3 questions (text, image, whatever fits your stimulus type) to present each stimulus. In each question's content field, insert the corresponding Embedded Data variable using this syntax:
- For the first stimulus:
${e://Field/Stim1} - For the second:
${e://Field/Stim2} - For the third:
${e://Field/Stim3}
That's it—each participant will get a unique, valid set of stimuli in random order.
4. Alternative: Real-Time Constrained Stimulus Generation
If you'd rather generate stimuli one at a time (instead of picking a pre-made set) to ensure no repeats, here's how to do it:
- Initialize Embedded Data variables
availableLettersandavailableNumbersin your Survey Flow. - Add this JavaScript to each stimulus question (it will dynamically generate the next valid stimulus):
// Initialize available letters/numbers only once (first question) if (!Qualtrics.SurveyEngine.getEmbeddedData("availableLetters")) { Qualtrics.SurveyEngine.setEmbeddedData("availableLetters", JSON.stringify(["a", "b", "c"])); Qualtrics.SurveyEngine.setEmbeddedData("availableNumbers", JSON.stringify(["1", "2", "3"])); } // Grab current available options let letters = JSON.parse(Qualtrics.SurveyEngine.getEmbeddedData("availableLetters")); let numbers = JSON.parse(Qualtrics.SurveyEngine.getEmbeddedData("availableNumbers")); // Pick random letter and number from remaining options const letterIdx = Math.floor(Math.random() * letters.length); const numberIdx = Math.floor(Math.random() * numbers.length); const selectedLetter = letters[letterIdx]; const selectedNumber = numbers[numberIdx]; // Update the question with the new stimulus document.querySelector(".QuestionText").textContent = selectedLetter + selectedNumber; // Remove used options so they don't repeat letters.splice(letterIdx, 1); numbers.splice(numberIdx, 1); Qualtrics.SurveyEngine.setEmbeddedData("availableLetters", JSON.stringify(letters)); Qualtrics.SurveyEngine.setEmbeddedData("availableNumbers", JSON.stringify(numbers));
This way, each time a participant moves to the next question, the script pulls from only the unused letters and numbers—guaranteeing no repeats.
内容的提问来源于stack exchange,提问作者Rtist

