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 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, d (the name of the variables is essential). Let us write the code.

quizzes.fractions_random_code.calc.png

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.

quizzes.fractions_random_code.2.calc.png

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.

sum_of_two_fractions_random_correct_answer.png

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, 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_fractions_random_statement.png

Sum of two random irreducible fractions

Notice that the algorithm could generate some unwanted statements we have suggested above as

sum_of_two_random_simple_fractions_1.png

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.

quizzes.fractions_random_den_code.calc.png

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

sum_of_two_random_simple_fractions_2.png

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.

quizzes.fractions_random_simple_code.calc.png

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.

quizzes.round_truncate_code.calc.png

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.

quizzes.round_truncate_code.2.calc.png

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.

round_example_2.png

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

quizzes.round_truncate_code_improved.calc.png

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

round_example_3.png

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,bLaTeX a,b and c of the expression a·x+b=c so we can create a set with the four inequalities as we see below.

quizzes.inequation_solver_code.calc.png

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

quizzes.inequation_solver_code.2.calc.png

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.

quizzes.derivate_evaluation_code.calc.png

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

quizzes.derivate_evaluation_code.2.calc.png

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 3x3LaTeX 3x3 integer matrix where its coefficients are absolutely random. First, we need to write the algorithm of the question.

inverse_matrix_define_1.png

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

inverse_matrix_define_2.png

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.

quizzes.inverse_matrix_code.calc.png

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.

quizzes.inverse_matrix_code.2.calc.png

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·x=bLaTeX A\cdot x=b solution, being ALaTeX A a random 3x3LaTeX 3x3 matrix and bLaTeX b a random vector. First, we need to write the algorithm of the question in a similar way as the last one.

quizzes.system_resolution_code.2.calc.png

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

quizzes.system_resolution_code.calc.png

Notice the possibility of the answer vector xLaTeX 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 ALaTeX A as a matrix with determinant 1 as we can see in the next example.

system_resolution_definition_2.png

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 ALaTeX A and the vector bLaTeX b, we will define our solution and then find an integer matrix ALaTeX A and an integer vector bLaTeX b satisfying A·x=bLaTeX A\cdot x=b.

quizzes.system_resolution_code_improved.calc.png

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.

quizzes.polynomial_roots_code.calc.png

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.

polynomial_roots_example_1.png

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.

quizzes.polynomial_roots_code_improved.calc.png

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

polynomial_roots_example_2.png

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.