Variable containing random integer within range

Hi, I think there is an issue with this code: the Round() cuts the density of marginal numbers (here 1 and 8) in half (see mini simulation in PDF attached). I think the code should be: (int)Math.Floor((9-1)*Quest.IRnd() + 1)

randomization.pdf (113.1 KB)

Could you please be more specific? What is the issue?

In other words, the code that was provided does not seem to select numbers 1 to 8 with the same probability, but 1 and 8 (the largest and smallest) get half the probability of the other numbers.

Cool. Where in the above the same probability was requested?

Sergiy, in my understanding someone asking to generate a random number between 1 and 8 in a CAPI context would want them to have equal likelihood.

To reply to your (I think rhetoric) question: where in the above was requested “a random number where the first and last occur half as often as the others”?

Bottmline, I was just flagging this such that others do not repeat a mistake we did…


This is my understanding too. But people are different. Angkor originally asked:

The solution mentioned by Misha above satisfies that:
1) it results in a number that is random;
2) the number is between 1 and 8
3) the number is an integer

Those interested in details about how computers operate with random numbers may refer to e.g. this very nicely written chapter:
(see paragraph 2.7).

Assuming that the original poster indeed wanted equal probabilities for each outcome, the critique of @FMBARBA relies on Quest.IRnd() to have equal probabilities of 0.1 and 0.2 and other numbers in his/her simulations. Whether it is true or not is a different question all together. Survey Solutions relies on C#'s built-in random number generator (which generates random numbers uniformly distributed on the interval [0;1) ):

        public static double GetRandomDouble(this Guid id)
            Random r = new Random(id.GetHashCode());
            return r.NextDouble();

It’s properties are sufficient to make a conclusion, that if you start from a random point determined from the id (seed) and take a million realizations, then the obtained sample will pass the uniformity tests (will look like a uniformly distributed random value).

But in practice, don’t forget, that Survey Solutions doesn’t do that. Instead a million random number generators are generated, and each generator is asked for a single random number. And in order to make them produce different results, the id of the interview is supplied. This brings in two potential problems:

  1. whether the GUIDs of the interviews are generated uniformly (I know they appear as text, but think of them as huge hexadecimal numbers)
  2. whether the algorithm that initializes random number generators in C# is somehow treating them unequally.

Both are beyond immediate intuition, they may or may not be true.

In addition, the random realizations here are not independent from the human will. An interviewer may instantiate an interview, discard it, and repeat, until she gets a number sufficiently close to the desired (and you can get an estimate of how many retries she needs for that).

So, whether values obtained from Quest.IRnd() in a large sample will follow a uniform distribution, is (imho) not guaranteed. Whether they are seen as such, you can ask other colleagues doing massive data collection, such as the users from Statistics South Africa or Zambia CSO.

In practice I would guide myself by the following rule: if the need for randomness is purely to make the questionnaire look differently, such as present the options in a different order, then none of the considerations above are of any importance. Good to go with Quest.IRnd(). But if your need for randomness is some sort of design experiment where rigorous assumptions about distributions are made and must be followed, then the question is not so simple.

So, no, not a rhetorical question.

Hi Support,
I have faced a smilar randomization issues - When Interviewer submits the completed questionnaire (say clinical vignettes I & II randomly selected for the health care provider and …completed interview) - after submission (synchronization). When supervisor opens up the assignment for review, the completed randomized (cases I & II ) changes to other randomized values say III & V … is there a fix for it? My randomization code used is as follows:

var r = (int)Math.Round((9-1)*Quest.IRnd() + 1);
var s = r.ToString(“0.0000000000”).Substring(2);
var items = new[]{

for (var i = 0; i < items.Length; i++)
    var n = Convert.ToInt32(s.Substring(i,1));
    if (n >= items.Length) n = 0;
    var t = items[i];
    items[i] = items[n];
    items[n] = t;

  //return items

string list_items = string.Join(", ", items);

return list_items

Your expertise or advice will be much much helpful in this regard!

Dear Sergiy,
This is to kindly inform that this problem isn’t solved yet. May be I not getting right solutions for this issues. If you can kindly help me to resolve the issues, please.
Thank you

Hello @dzangpo , your problem is likely different from the one reported fixed and is likely related to your code, not the Survey Solutions code.

Please read the ‘Instantiating the random number generator’ section here.

Hope this helps.
Best, Sergiy