How avoid zero (0) in random list in formulas

Re: How avoid zero (0) in random list in formulas

by Joachim Tropf -
Number of replies: 10
Picture of Particularly helpful Moodlers
Hi Dominique, hi Baltasar,

The arry in the  solution proposed by Dominique has a weak order (in the propsed case negative numbers first). Other split arrays won't solve this kind of order.
An approach to avoid this might be the following:

#Random variables
x=shuffle([-9:9]);    # 0 is in, but 9 not   

#Global variables
for (i:[0:18]) {x[i]=(x[i]==0)?9:x[i]};   # 0 is replaced by 9


This examlpe can as well easily be be adjusted to other problems (e.g. not 0, -1 or +1 in an array from -100 to +200!


Cheers
Joachim


Average of ratings: Useful (1)
In reply to Joachim Tropf

Re: How avoid zero (0) in random list in formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Joachim,

Thank you for pointing out this weakness in my solution.

Your solution is excellent, but here is a more general and perhaps more elegant one: smile

MoodleForum_20240329_0243.png

dynamiccourseware.org/ Formulas qtype - Documentation/ Functions / shuffle()➚.

In reply to Dominique Bauer

Re: How avoid zero (0) in random list in formulas

by Joachim Tropf -
Picture of Particularly helpful Moodlers
Hello Dominique,
Great and elegant solution!
With sublist() one gets a real random permutation/shuffle!
By the way: I never used sublist because there was no need for and did all the permutations with expessions like X[Y[i]]!
Thank you for pointing out the elegant use of this function!

Cheers
Joachim
In reply to Joachim Tropf

Re: How avoid zero (0) in random list in formulas

by Baltasar Ortega Bort -
Hi!
I like the last one. I understad that the function sublist takes the the list created in x and shuflled following the z numbers, don't?
One more thing. As I said, i'm a newbie and I have a couple of doubts because I'm a little crazy when it comes to searching for information about Formulas, although this forum is a well of wisdom.
Surely this will be answered somewhere in a precise way but:
(a) what language does Formulas use?
(b) And what is the best place, apart from https://dynamiccourseware.org, where I can find a reference to this language?
(c) what is the difference between random and global variables?
(d) How can I make the arithmetic operations with integers appear correctly, i.e., I don't get a "+-3" or a "+5" when the integer is positive at the beginning of the operation. I don't know if I make myself clear.
I take this opportunity to thank you both for your answers.
Regards
Baltasar


In reply to Baltasar Ortega Bort

Re: How avoid zero (0) in random list in formulas

by Joachim Tropf -
Picture of Particularly helpful Moodlers
Hi Baltasar,
I tried to write a paedagogicol  "Introduction to FORMULAS qt ( and JSXGraph)" that leads you from "Hello World"  some stages up!
You can find it here: https://moodle.jotro.net/course/view.php?id=46
BTW.: On this site there are as well more examples of FORMULAS questions (ready to copy and paste)  unfortunatelly in German sad. Since Mathematic has its universal language you may try as well these examples. If you have questions/problems or if you are interested in a translation of  specific passages (which Google translator can't handle?) feel free to contact me!

Cheers
Joachim  
Average of ratings: Useful (1)
In reply to Baltasar Ortega Bort

Re: How avoid zero (0) in random list in formulas

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

I understand that the function 'sublist' takes the list created in 'x' and shuffles it following the 'z' numbers, doesn't it?

That is correct.


(a) what language does Formulas use?

The 'Formulas' question is written in PHP, like the other question types and most of Moodle.

The user is provided with a set of functions to be used in the 'Random variables,' 'Global variables,' and 'Local variables' fields. Most of these functions are taken from PHP, while 'pick()' is a Qlik function, 'sublist()' is custom-made for a common programming task, etc.


And what is the best place, apart from https://dynamiccourseware.org, where I can find a reference to this language?

There may be other places, but I haven't made a list of them. You can search for them using Google Search.


How can I make the arithmetic operations with integers appear correctly, i.e., I don't get a "+-3" or a "+5" when the integer is positive at the beginning of the operation?

The 'poly()' function nicely formats polynomials.

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: How avoid zero (0) in random list in formulas

by Baltasar Ortega Bort -
gracias a los dos. La información que me proporcionáis es muy valiosa.
Joachim -> Aunque no sé alemán las excelentes ejemplos y los traductores en línea hacer maravillas.
Dominique -> Probaré 'poly()' function . Es posible que soluciones algunos problemas pero me temo que os voy a preguntar mucho.
Gracias y feliz domingo
In reply to Baltasar Ortega Bort

Re: How avoid zero (0) in random list in formulas

by Baltasar Ortega Bort -
Sorry. I post it in spanish:


Thank you both. The information you provide is very valuable.

Joachim -> Although I don't know German the excellent examples and online translators work wonders.

Dominique -> I will try the 'poly()' function. It may solve
some problems but I'm afraid I'm going to ask you a lot of questions.

Thanks and happy sunday
In reply to Baltasar Ortega Bort

Re: How avoid zero (0) in random list in formulas

by Baltasar Ortega Bort -
Hi:
Sorry but I can't reproduce what I want. The Poly function works fne but I don't know how works with it at answer word.
Let me explain what a wnat to dos, perhaps it more easy that I imagine.

I want that my students write algebraic formulas.

I create the following question:
--> Write a number plus 5
And the students must answer:
--> x + 5

This is easy. I can make
#Random variables
z = shuffle([1:18]);

# Question
--> Write a number plus {z}


# Answer
Type = Algebraic
Answer = ["x+z]"
Error abs < 0.1
Local variable = x={3};

Everything OK!

The problem arises with negative numbers,
With the Dominique's suggestion we can make a list without zero.

#Random variables
z = shuffle([1:18]);

#Global variables
a = [-10:-1];
b = [2:11];
x = concat(a,b);
y = sublist(x, z);


# Question
--> Write a number plus {y}

The solution could be x-2
So:
a) I can't write the question: Write a number plus {y} because "plus" could be "minus"
b) I can't put the answer: ["x+z]" because it appears x+-2

I try make this:
#Random variables
z = shuffle([1:18]);

#Global variables
a = [-10:-1];
b = [2:11];
x = concat(a,b);
y = sublist(x, z);
z = y[0]
sig = (z <0) ? 0:1;
signo =["minus","plus"][sig];
z_abs = abs (z);

# Answer
Write a number {signo} {z_abs}

But I don't know how to correctly evaluate the answer because sometimes is "+" and sometimes is "-".

Can you help me?

Thanks in advanced.
In reply to Baltasar Ortega Bort

Re: How avoid zero (0) in random list in formulas

by Joachim Tropf -
Picture of Particularly helpful Moodlers
Hi Baltasar,
One solution may be to select positve numbers <>0 at first hand.
Then chose randomly plus or minus (I do it with pm={0,1} in random variables).
Then pick the word "plus" or "minus" in global variables using
signo=pick(pm,["plus","minus"];
as well as the solution:
ANS=pick(pm,["x+zz","x-zz"]) ;    
But note, that any equivalent term will give the full mark, e.g. x--2, or x+1+1 in case of x+2 was right!
Cheers
Joachim

Average of ratings: Useful (1)
In reply to Joachim Tropf

Re: How avoid zero (0) in random list in formulas

by Baltasar Ortega Bort -
Hi Joachim!
It works perfectly.
I put ANS like a Algebraic answer.
Thank you