# Advanced use of random variables

On this page, we will see the wide range of possibilities that the `random`

command offers when creating questions with WirisQuizzes through some examples. More detailed information about how to create the questions in WirisQuizzes 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.

### Tip

You can find more detailed information about how to create this question with the basic mode here.

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 `a`

, `b`

, `c`

, and `d`

(the name of the variables is essential). 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 solution in the *Correct answer* tab. Now it is not a number; it is a variable 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 question's behaviour 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`

, and `den2`

. In the same way, as in the *Correct answer* section, 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 the algorithm could generate some unwanted statements 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 that 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 truncate some random numbers to the hundredth ones. We will choose the embedded answers (Cloze) question type. Let's write the algorithm of the question.

### Tip

You can find more detailed information about embedded answers (Cloze) here.

The random command allows us to retrieve a random number in a given interval. Actually, we can add an option, so just the interval numbers 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.

### Caution

Notice the importance of using the sign `:=`

instead of `=`

when defining the function `r()`

. Otherwise, `r()`

will always take the same value, and the five numbers will 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.

### Note

Notice that we multiplied the rounded and truncated numbers by `1.0`

, so the answers appear in decimal form instead of the rational form.

As we have written the algorithm, we can obtain some unwanted numbers which are already rounded at the tenth one or truncated at the hundredth one, as we can see in the example below.

To avoid this kind of situation, 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 $a,b$ and $c$ of the expression $a\xb7x+b=c$ 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.

### Caution

Recall we need to precede the name of a variable with the pound sign `#`

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 at 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.

### Caution

Recall we need to precede the name of a variable with the pound sign `#`

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 $3x3$ 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.

### Tip

You can find more detailed information about the different CalcMe programming options here.

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

### Caution

Recall we need to precede the name of a variable with the pound sign `#`

every time we want to call it outside CalcMe. Thus, we must 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 $A\xb7x=b$ solution, being $A$ a random $3x3$ matrix and $b$ 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 $x$ 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 $A$ 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 a random matrix since we find one with determinant 1, which is very inefficient. In order to optimize our algorithm, we should change our approach to the question. Instead of defining randomly the matrix $A$ and the vector $b$, we will define our solution and then find an integer matrix $A$ and an integer vector $b$ satisfying $A\xb7x=b$.

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.

### Caution

Recall we need to precede the name of a variable with the pound sign `#`

every time we want to call it outside CalcMe. Thus, we must 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 the algorithm could generate unwanted polynomials, whether it lacks real roots or the difficulty of finding them by hand, as we can see in the following example.

### Tip

You can find more detailed information about answers in lists or sets mode here.

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.

### Caution

Recall we need to precede the name of a variable with the pound sign `#`

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

in the correct answer tab and `#p`

in the statement.