Create a list of random numbers

How can I create a list of integers that randomly order 8 numbers. I’m trying to build my code based on your “publicExampleRandomizingOrder”, but I am not able to create my “list variable”

My [double] variable where I intend to save the list “items” is:

(new Func<double>( () => {
    $randomize;
})).Invoke()

My macro $randomize is:

  var r = Math.Log(1 + Math.Floor(Quest.IRnd()*1000000)/1000000); 
  var s = r.ToString("0.0000000000").Substring(2);
  var items = new[]{
     1,
     2,
     3,
     4,
     5,
     6,
     7,
     8 };
     
    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

Once again, thank you for your precious help

Survey Solutions does not have variables of type list. A variable may be of type:

  • Boolean,
  • Long,
  • Double,
  • DateTime, or
  • String.

(technically the nullable versions of each of the above).

No wonder if you try to define a list, array, collection, or other complex structure it will reject it:

Leave aside the solution that you’ve found and describe the problem first. Instead of “create a list of integers that randomly order 8 numbers” your supervisor has probably told you to “present household members in random order”, or “present categories of a single-select question in random order”, or “select three random females”, or something else. It is important.

It is equally important what is the data structure that you are working with. If you are randomly selecting a member, what is the structure that holds the members? what are the roster and variable names, question types and parameters, etc.

I am leaving aside the randomness issues (like how random should the distribution be? do you just need some variation?) or even why do you need to generate it in the questionnaire itself?? I hope this will become obvious from other descriptions you provide, though not obvious at this time.

In short: the above code may not work without a context, but once you describe the context, there may be a totally different (and hopefully simpler) solution, that may help avoid that code’s complexity.

1 Like