# Use of random variables

`random`

command offers when creating questions with Wiris Quizzes through some examples. More detailed information about how to create the questions in Wiris Quizzes basic guide## Randomness in intervals and sets of numbers

### Sum of two random fractions

We will create a question asking the students for the sum of two random fractions. First, we need to write the algorithm of the question.

The `random`

command allows us to retrieve a random number in a given interval. We need four random numbers: two numerators and two denominators; that we will call `num1`

, `num2`

, `den1`

, `den2`

(the name of the variables is important). Let us write the code.

Now we have four random numbers between 1 and 9 (both included). We will also compute the sum of the fractions they define and store the result in a variable.

Once we have defined the algorithm, we write the question's solution in the *Correct answer* tab. Now it is not a number, it is a variable that we called `sol`

. In order to write a variable anywhere outside CalcMe, we must precede the name of the variable with the pound sign `#`

. Therefore, we will write `#sol`

in the correct answer field.

Finally, once we have checked the behaviour of the question in the *Preview* tab, we write the statement of the problem. Recall that our numerators and denominators were stored in variables called `num1`

, `num2`

, `den1`

, `den2`

. In the same way, as in *Correct answer* tab, we have to write the name of the variables preceded by the pound sign `#`

. Fractions can be written with MathType.

### Sum of two random irreducible fractions

Notice that some unwanted statements could be generated by the algorithm we have suggested above as

where both fractions have the same denominator. We can avoid this situation with a simple change in our algorithm, we just have to exclude the first denominator value from the set of the second denominator possible values. Here we can see how the code will look like.

Furthermore, the initial algorithm could generate even more undesirable statements, as we can see in the following example.

In this case, the fractions appeared in the statement are not simplified and they can even be expressed in integer form. To prevent this situation, the values whose greatest common divisor with its numerator is different from 1 will be excluded from the set of possible denominator values. Here we can see how the code will look like.

### Rounded and truncated random numbers

We will create a question asking the students to round to the tenth ones and to truncate to the hundredth ones some random numbers. We will choose the embedded answers (Cloze) question type, let's write the algorithm of the question.

The random command allows us to retrieve a random number in a given interval, actually, we can add an option so just the numbers of the interval with a given step will be considered as we will see below.

We need five numbers with three decimal digits each. Since it could result in a little bit tedious to write so many times that command, we will avoid it by defining a global function `r():=random([0..10..0.001])`

. Let us write the code.

`:=`

instead of `=`

when defining the function `r()`

. Otherwise, `r()`

will always take the same value and the five numbers would be the same. More detailed information about creating custom functions here
For now, we have five real random numbers between zero and ten with three decimal digits. We also have to round them to the tenth ones and truncate them to the hundredth ones.

`1.0`

so the answers appear in decimal form instead of in the rational form.
As we have written the algorithm, we can obtain some unwanted numbers which are already rounded at the tenth ones or truncated at the hundredth ones as we can see in the example below.

To avoid this kind of situations, we can prevent numbers ended with 0 or 00 from being generated in the following way.

This way we will only obtain appropriate numbers with three decimal digits as we can see below.

## Randomness in generic sets

### Inequations resolution

We will create a question asking the students to solve first-grade inequations with random coefficients and random inequalities. First, we need to choose the coefficients and of the expression so we can create a set with the four inequalities as we see below.

Once we have defined the set, we need to write the algorithm of the question with its solution.

`#`

every time we want to call it outside CalcMe. Thus, we have to write `#sol`

in the correct answer tab and `#f`

in the statement.
### Derivative random function in a random point

We will create a question asking the students for the value of the derivative of a random function in a random point. First of all, we have to decide in which set could our function be stored. Here you can see an example.

Once we have defined the set, we need to write the algorithm of the question with its solution.

`#`

every time we want to call it outside CalcMe. Thus, we have to write `#sol`

in the correct answer tab and `#f(x)`

and `x0`

in the statement.
## Programming

### Calculus of the inverse of a random matrix

We will create a question asking the students for the inverse of a integer matrix where its coefficients are absolutely random. First, we need to write the algorithm of the question.

We can also create the matrix in a more esthetic and compact way as is shown in the example code below.

Notice that, if we want the matrix to be invertible, we need its determinant to be different from zero, which is possible in our two initial proposals. In order to avoid this situation, we need to use the programming resources available in CalcMe as we can see in the following example.

Furthermore, we will write the algorithm as we can see in the following example.

`#`

every time we want to call it outside CalcMe. Thus, we have to write `#sol`

in the correct answer tab and `#A`

in the statement.
## Variables from the answer

### Random lineal system resolution

We will create a question asking the students for the system's solution, being a random matrix and a random vector. First, we need to write the algorithm of the question in a similar way as the last one.

The question's solution will be given in the following way.

Notice the possibility of the answer vector not being in integer form. Theoretically, this should not be a problem for the students, but we can be interested in an integer answer to make the calculus made by the students less tedious. In order to solve this problem, we can define as a matrix with determinant 1 as we can see in the next example.

Thus, even though we manage to arrange the problem, we are generating random matrix since we find one with determinant 1, which is very inefficient. In order to optimize our algorithm, we should change our approach of the question. Instead of defining randomly the matrix and the vector , we will define our solution and then find an integer matrix and an integer vector satisfying .

This new perspective about how we are defining the question's random parameters will allow us to control how the student answer is going to be.

`#`

every time we want to call it outside CalcMe. Thus, we have to write `#sol`

in the correct answer tab and `#A`

, `#b`

in the statement.
### Random polynomial roots

We will create a question asking the students for the roots of a random polynomial with a random degree. First, we need to write the algorithm of the question.

Notice that unwanted polynomials could be generated by the algorithm, whether its lack of real roots or the difficulty of finding them by hand as we can see in the following example.

In order to arrange it, we can define the polynomial as a product of its roots as in the example below.

This way all the polynomial roots are integers and the students will be able to find them as we can see then.

`#`

every time we want to call it outside CalcMe. Thus, we have to write `#sol`

in the correct answer tab and `#p`

in the statement.