Each variable should only occur once?

vtselios7

Member
Joined
Feb 17, 2023
Messages
6
Programming Experience
Beginner
Hello, i hope iam posting this question in the right place.Please forgine me if not. I am using an online software for statistics (survey solutions designer) that can run code. I have the above code that works fine.
C#:
$q5rand
var r = Math.Log(1 + Math.Floor(Quest.IRnd()*1000000)/1000000);
var s = r.ToString("0.0000000000").Substring(3);
var items = new[]{"item1","item2","item3"};
 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
(new Func<string>( () => {
    $q5rand[0];
})).Invoke()

(new Func<string>( () => {
    $q5rand[1];
})).Invoke()

(new Func<string>( () => {
    $q5rand[2];
})).Invoke()[/I][/I]

This code returns 3 variables in a questionnaire. The problem is that the probability of occurrence of each variable is not equal. I want the probability to be equal and also the occurrence to be unique. Each variable should only occur once.
Any help would be appreciated. Thanks in advance!
 
Last edited by a moderator:
Welcome to the forum.

In the future please post your code in code tags. The easiest way to do that is to use the button on the toolbar that looks like </>. Code tags preserve the indents of your code as well escapes any strings that look like BBCode which is the markup used on this forum.

I tried to repair your post above as best as I could, but parts of it still look wrong. Sorry.

As for your question, the $q5rand does not look to be a valid C# identifier name. You said that your code works fine, so this is kind of puzzling.

As for that unusual distribution, we have no idea what Quest.IRnd() does. Are you sure that it had even distribution? Do you have the source code for it that you can share with us? In general, most random number generators on computers are pseudo random number generators, and their distributions maybe be skewed, or the math used to limit values to a range may skew them.
 
Welcome to the forum.

In the future please post your code in code tags. The easiest way to do that is to use the button on the toolbar that looks like </>. Code tags preserve the indents of your code as well escapes any strings that look like BBCode which is the markup used on this forum.

I tried to repair your post above as best as I could, but parts of it still look wrong. Sorry.

As for your question, the $q5rand does not look to be a valid C# identifier name. You said that your code works fine, so this is kind of puzzling.

As for that unusual distribution, we have no idea what Quest.IRnd() does. Are you sure that it had even distribution? Do you have the source code for it that you can share with us? In general, most random number generators on computers are pseudo random number generators, and their distributions maybe be skewed, or the math used to limit values to a range may skew them.

thank you!
 
Anyway that seems to be an over complicated way to shuffle items in an array. Read about Fisher-Yates which is much more efficient.

Also a better way to get a random number from 0-N (inclusive) is to use something like Math.Round(Quest.IRnd() * N). No need to do any of the generating a string and then trying to part out characters.

Just copy the question response choices into your items array. After you have shuffled the items array, you should just be able to return the array itself. At least that is my guess. I can't find the full documentation for that software you are using.
 
Anyway that seems to be an over complicated way to shuffle items in an array. Read about Fisher-Yates which is much more efficient.

Also a better way to get a random number from 0-N (inclusive) is to use something like Math.Round(Quest.IRnd() * N). No need to do any of the generating a string and then trying to part out characters.

Just copy the question response choices into your items array. After you have shuffled the items array, you should just be able to return the array itself. At least that is my guess. I can't find the full documentation for that software you are using.

thanks for your post, i could find the full documentation also. i will search about Fisher-Yates.
 
Almost. According to the scant documentation available for the software in which the code is running in, the Random class does not work properly and hence why the software maker offers up their Quest.IRnd() method. Replacing the call to Next() should work.
 
Almost. According to the scant documentation available for the software in which the code is running in, the Random class does not work properly and hence why the software maker offers up their Quest.IRnd() method. Replacing the call to Next() should work.

Thanks for all your help. i will try it and i hope it works.
 
Back
Top Bottom