Return a random discrete number between 0-100 from a geometric distribution | XM Community
Skip to main content
Solved

Return a random discrete number between 0-100 from a geometric distribution

  • June 6, 2020
  • 2 replies
  • 12 views

Hello, I want to first get a random discrete number between 0-100 from a geometric distribution, and then store it as an embedded data.
1) At the beginning of the survey flow, I created an embedded variable: result_q
2) In the first question, I have the following have code:
Qualtrics.SurveyEngine.addOnload(function()
{
var min = 0;
var max = 100;
var prob = 0.1;
function geoDist(min, max, prob) {
  var q = 0;
  var p = Math.pow(prob, 1 / (max - min));
  while (true) {
  q = Math.ceil(Math.log(1-Math.random()) / Math.log(p)) + (min - 1);
  if (q <= max) {
  return q;
Qualtrics.SurveyEngine.setEmbeddedData('result_q', q);
}
}

}

});

When I paste ${e://Field/result_q} in the next question I don't get any result. Could anyone help me please? If you have other ways of getting the number, also welcome.
Thank you!

Best answer by TomG

You never called the geoDist function. Try:
Qualtrics.SurveyEngine.addOnload(function() {
var min = 0;
var max = 100;
var prob = 0.1;
Qualtrics.SurveyEngine.setEmbeddedData('result_q', geoDist(min,max,prob));

function geoDist(min, max, prob) {
  var q = 0;
  var p = Math.pow(prob, 1 / (max - min));
  while (true) {
  q = Math.ceil(Math.log(1-Math.random()) / Math.log(p)) + (min - 1);
  if (q <= max) {
  return q;
}
}
}
});

2 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • June 6, 2020

You never called the geoDist function. Try:
Qualtrics.SurveyEngine.addOnload(function() {
var min = 0;
var max = 100;
var prob = 0.1;
Qualtrics.SurveyEngine.setEmbeddedData('result_q', geoDist(min,max,prob));

function geoDist(min, max, prob) {
  var q = 0;
  var p = Math.pow(prob, 1 / (max - min));
  while (true) {
  q = Math.ceil(Math.log(1-Math.random()) / Math.log(p)) + (min - 1);
  if (q <= max) {
  return q;
}
}
}
});


  • Author
  • June 6, 2020

Thank you Tom! it works!