Is there a way to record which random number was generated by the random integer command? | XM Community
Solved

Is there a way to record which random number was generated by the random integer command?

  • 23 April 2018
  • 4 replies
  • 22 views

Badge +3
I have a survey where I am generating a random number between 1 and 6 (${rand://int/1:6}) in loop and merge and piping it from loop and merge into the text of a question. I want to record what random number was was presented in each of the loops. Right now I have JS code that is saving the piped text from the loop and merge into an embedded data field, but it looks like it is saving a new random number to the embedded data field and not the number that was presented.
Does anyone know if there is a way to record the actual number that was shown/generated, rather than a new random number?
icon

Best answer by TomG 24 April 2018, 17:03

View original

4 replies

Userlevel 7
Badge +20
Instead of saving the piped text into an embedded data field, I would recommend to create an open end question within the same loop and merge block (so that you will have an equivalent number of an open-end question) and save the piped-text value in it through JS code. Also, please hide this open-end question from the screen. Its must that the question in which you are capturing the value through JS should get executed hence please do not hide it through skip/display/ branch logic.

Please try and let me know...
Userlevel 7
Badge +27
@mcoverdale - You're saying the value of the piped field (e.g. $lm://Field/2) changes in the same loop? That doesn't sound right.

Anyway, an approach that will guarantee that you save the same number presented...

Put your piped random number inside a span with an id:
```
<span id="myRandNum">${lm://Field/2}</span>
```
Then have your JS save the contents of your span to an embedded data field:
```
Qualtrics.SurveyEngine.setEmbeddedData(${lm://CurrentLoopNumber}+"_myRandNum",jQuery("#myRandNum").text());
```
Be sure to initialize your embedded data fields (e.g., 1_myRandNum, etc.) in the survey flow.
Badge +3
@TomG
Sorry that my wording was a bit confusing; the number didn't change within the same loop but across the different loops such that for each loop a random number 1-6 was presented (the problem was that when I was saving it to the embedded data field it wasn't saving the same number that was presented).

Your code works great! Because I have so many loops, I changed the JS code a bit to try to record each of the values into one embedded data field. Oddly now it is recording the first number presented twice and skipping the last one (all the rest are recorded correctly), but I think I should be able to fix that.

Thanks for your help!
Badge +3
@Mohammedali_Rajapakar_Ugam
That is a really clever suggestion!
Unfortunately, when I tried it the value that shows in the question text is different from what is shown in the open-ended question. I think it's weird that this would happen, given that they are piped from the same field (${lm://Field/1}) during the same loop so they should be the exact same number. Below is what it looks like (I have the open-ended question visible so you can see the number).
!
Thanks for the suggestion though!

Leave a Reply