Moodle Formulas question type - restricting variable instances

Moodle Formulas question type - restricting variable instances

από Chris Munro -
Αριθμός απαντήσεων: 12

Hi,

I'm new to the Moodle Formulas question type and am testing it/comparing it with STACK before installing one of these on our production site.

I've made some simple questions using Formulas (including some with embedded graphs using JSXGraph), but one thing I would like to be able to do is restrict random instances of the variables and I can't see in the documentation how this could be achieved.

For instance, I have a simple question asking students to expand brackets to give an algebraic expression. The question contains 3 local variables a, b & c and I've defined the sets of possible values that these variables can take. I want the set of possible values for c to be identical, but I don't want = c in any specific instance. Can I achieve this in Formulas? I see there is a does not equal operator (!=) but it throws up a syntax error when I try to set a global variable b != c;

As a side note/follow up to this, in what circumstances/syntax can I use the comparison operators, e.g., less than, greater than, does not equal, etc.? I've tried using them in local and global variables but they always throw up syntax errors, e.g., b < c;, or b != c;.

Thanks in advance for your help!

Μέσος όρος βαθμολογίας: -
Σε απάντηση σε Chris Munro

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers
Hello Chris,

... I don't want b = c...
You can do it in different ways, for example with the shuffle() or the pick() function. It may depend on how your variables are defined. Please attach an XML file of your question together with some explanations. I will look at it next week. Other people might also in the meantime.

In what circumstances can I use the comparison operators?
You can use these in the conditional (ternary) operator. See https://moodleformulas.org/course/view.php?id=100&section=3
Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Chris Munro -
Thanks Dominique! I can see now when I can use the conditional operators.

I've attached the XML below. I've thought about it some more and I've found a further wrinkle that I hadn't considered previously when asking the question.

The quiz question asks a student to expand the brackets in an expression of the type a(bx+c), where the expected answer is an algebraic expression with some x term and a constant. I've used 3 local variables for aa, bb & cc and want them to take values from similar ranges (aa={2:7}; bb={1:10}; cc={1:11};). But I really don't want bb and cc to take the same value because it doesn't help reinforce the concept that a student has to multiply both aa*bb and aa*cc. Also, the question looks odd when bb and cc are the same value.

However, I've now realised there is one occasion where it would be ok to have bb=cc and that is if bb=cc=1. I had thought to limit the instances that cc could take based on the value of bb, i.e., cc != bb, but I'm not sure how to achieve that. Any help is much appreciated!
Σε απάντηση σε Chris Munro

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers

Hello Chris,

You can try the question below at https://moodleformulas.org/course/view.php?id=78&section=77 ↗, where you will also find the XML file for the question (also attached below).

I added explanatory comments in the question. Do not hesitate to ask me for clarifications if necessary.

MoodleForum_20220829_2230.png

Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Chris Munro -
Thanks Dominique! I could not have come up with that script by myself. I especially like the handling to enable bb=cc=1.

I'm going to use it as is but I can see 1 weakness to it - if I want to change the range of possible bb (or cc) values, I would have to adapt the shuffle list accordingly. I like Joachim's simple idea below, although it would be great if the added number in the true value ("bb+2") could be dynamic so that it could fit the range of possible cc values.

I was musing a solution to that and saw your example of "Add the first n integers", and thought I might be able to use that to accomplish it. Alas, I think it might be impossible because the fill() function doesn't allow a variable for the first argument.

Thanks for your help!
Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Joachim Tropf -
Φωτογραφία Particularly helpful Moodlers
Hi Dominique and Chris,
I do avoid identical values on variables by the simple line
bb=(bb==cc)?bb+2:bb;
in the global variables partlächelnd
Σε απάντηση σε Joachim Tropf

Re: Moodle Formulas question type - restricting variable instances

από Chris Munro -
Hi Joachim,

Thanks for your input! I like the simplicity, and it's similar to the way I was thinking.

It would be great if the number in the true value argument "bb+2" could be made dynamic to better reflect the intended range of possible bb values. Something like, "bb+n" where n is a list of numbers within the range of bb but not equal to cc. Alas, I'm not sure it's possible.
Σε απάντηση σε Chris Munro

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers
Hello Chris and Joachim,

Joachim's method gives two different numbers, but always with an interval of two, which may not always be what you want.

To get two different random numbers, use a shuffled list. For example, for two different random numbers between 1 and 10, use:

Random variables
list = shuffle([1:11]);

Global variables
a = list[0];
b = list[1];

For three random numbers:
a = list[0];
b = list[1];
c = list[2];

The indices don't matter as long as they are different. For instance
a = list[2];
b = list[4];
c = list[7];
will also give three different random numbers (but 0, 1, 2 is what comes to mind first).

If you want two different random numbers in the range 1 to 10, but two occurrences of the number 7 is allowed, use:
list = shuffle([1,2,3,4,5,6,7,7,8,9,10,11]);
where the number 7 appears twice in the list.
Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers
Do not hesitate to ask for explanations if necessary concerning my previous post.

Corrigendum

If you want two different random numbers in the range 1 to 10, but two occurrences of the number 7 is allowed, use:
list = shuffle([1,2,3,4,5,6,7,7,8,9,10]); # Do not include 11


Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Chris Munro -
Thanks again, Dominique! Yes, that sums up the situation well.

Joachim's solution is simple and intuitive, but the weakness is that the random numbers will be given an interval of 2 if they come up the same, and could potentially result with a value outside of the intended range. Also, it doesn't allow for b and c to both equal 1.

Your solution is less intuitive (to me at least) but solves those issues. In solving those, it introduces the limitation that b and c can't have independent ranges of potential values, if I so wished. It appears to me that there is no way to solve all the issues at once (because the fill() function doesn't allow for an array of n values), and so there will always be a trade off - that's ok, your solution works and that is the most important thing.
Σε απάντηση σε Chris Munro

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers
... it introduces the limitation that b and c can't have independent ranges of potential values...
Probably doable. Use two different lists, then check if the values are the same. If so, choose the next value from the longest list. I will check later.
Σε απάντηση σε Chris Munro

Re: Moodle Formulas question type - restricting variable instances

από Dominique Bauer -
Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Plugin developers

Hello Chris,

Say you want to expand a(bx+c) where:

  • a is a random integer between 2 and 6,
  • b is a random integer between 1 and 10,
  • c is a random integer between 1 and 20.

You want b and c to be different, except they can both be 1 simultaneously.

Proceed as follows:

Random variables
# list1  shuffled list of integers from 2 to 6;
# list2  shuffled list of integers from 1 to 10;
# list3  shuffled list of integers from 1 to 20;
list1 = shuffle([2:7]);
list2 = shuffle([1:11]);
list3 = shuffle([1:21]);
Global variables
# a  random element of list1 ;
# b  random element of list2 ;
# c  random element of list3 ;
a = list1[0] ;
b = list2[0] ;
c = list3[0] ;
#  b and c must different, except they can both be 1 simultaneously: 
c = ( b !=c ) ? c : ( ( b == 1 ) ? 1 : list3[1] ) ;

I don't know if it's intuitive, but it looks pretty simple to me. χαμόγελο

You can try this question at https://moodleformulas.org/course/view.php?id=78&section=77 ↗.

Σε απάντηση σε Dominique Bauer

Re: Moodle Formulas question type - restricting variable instances

από Chris Munro -
Dominique,

That looks perfect! Anyone could pick up that question and use it as is or, if they want to make it a little easier/harder, modify the list ranges to suit their needs without having to change any of the rest of the code. That looks exactly like I had envisaged when I started out...no, when I thought through it more clearly. Sorry, I didn't articulate it like that earlier!

And, as a bonus, I've learnt that you can (and how to) nest conditional statements in Formulas.

Thank you, you're a champion!