Displaying a random number within question (and choice options) using JavaScript | XM Community
Skip to main content
Question

Displaying a random number within question (and choice options) using JavaScript

  • November 6, 2019
  • 4 replies
  • 39 views

Hello everyone :) I am trying to achieve the following and I am assuming that I will need some JavaScript for that, however, I am not (yet) familiar with that. So really any hint of help is very much appreciated! Within the description text of a question I would like to display a random number between two values (newly drawn for every participant). Within a choice option of that question I would like to do the same thing but with different min and max values. Additionally, I also need to be able to record which numbers were drawn for each participant in each question. More specifically, for the question text I need the text to look something like this: "texttextext (randon number)€. texttextext" I am assuming one can use some form of this code: var randomnumber = Math.floor(Math.random() * (500 + 1) + 50) but than again my understanding of JavaScript is fairly limited. Thank you so much for sharing your ideas! Marco

4 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • November 6, 2019
Marco, Since you need to save the values, you can just pipe a random number into an embedded variable in the survey flow, then pipe the embedded variable into the question. No JS needed. ``` Q1_rand = ${rand://int/5:55} ```

Hi Tom, thank you very much for your quick help - I totally didn't know about the embedded variable and piping functions. I do have a follow-up question though: is there any way to take an embedded variable as the upper limit of a random number range? Something like this: ${rand://int/ 5 : e://Field/VariableName} I am assuming that the random number function needs two integers to work - can I make the embedded variable an integer somehow? Thank you!

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • November 7, 2019
No, you can't nest piped values.

Thanks for your answer. Do you see any other possibility to achieve the following: I need to draw a random number X. Next I need to draw a second random number from a different range - more specifically the upper level will either be Y or if the first drawn number (X) is lower than Y - X needs to be the upper limit. So it is basically an if than statement (which is technically possible with the branch function) - however the upper limit under the condition X<Y needs to be X aka a randomly drawn number itself.