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

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


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!

icon

Best answer by TomG 6 June 2020, 14:40

View original

2 replies

Userlevel 7
Badge +27

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;
}
}
}
});

Thank you Tom! it works!

Leave a Reply