## Question Authors' Forum

### Question with a random variable

Question with a random variable

Hello,

it is possible with coderunner to ask a question where the answer is a simple line of text (not a program).
i would like to know if it is possible to specify a random value in the question ?

For example :

What is the value of a modulo b ?

and a and b are randomly generated when the student chooses the question.

Thanks.

Re: Question with a random variable

I think this is possible using CodeRunner, if you use a custom template. However, thinking of Moodle as a whole, it is probably not the best way.

In standard Moodle you could use the Calculated question type for this: https://docs.moodle.org/32/en/Calculated_question_type

If you want more sophisticated randomisation and mathematics, then look at STACK https://moodle.org/plugins/qtype_stack

Re: Question with a random variable

Currently, the question text for CodeRunner is constant, so you can't vary it from one student to the next. However, David Bowes at University of Hertfordshire has forked CodeRunner and added the ability to apply Twig template expansion to all question attributes. This lets you insert variables, which could be pseudo-randomly generated, into the question text and the associated test cases, standard input, etc.

David has also prototyped a more major extension, which allows him to run a scenario-generating script when the question is first prepared for a particular student. This gives you almost unlimited power to dynamically generate questions and matching test data.

I'm not at this stage sure to what extent I wish to incorporate these changes into the CodeRunner master branch. They do add significant complexity to what is already a pretty complex question type, and they make testing and validation of a question much more problematic. Like Tim, I'm inclined to think that other question types might better handle the sort of question you use as an example. Or you could create a pool of very similar questions and use the quizzes "add-a-random-question" capability to pick questions from that pool.

All the same, there are some use cases for which some form of randomisation or scenario-generation within CodeRunner would be useful. I'd like to hear about more of these, to help me make up my mind.

Richard

Re: Question with a random variable

Thanks a lot for your answers. I have already used the STACK plugin. Unfortunately it is not possible to easily manage array of strings with this plugin, it works well for numerical answers not for text based answers. Create a pool of very similar question is not an option for me. In fact i would like to be able to randomly generate a value in a question (numerical value or string value) and then to be able to write an algorithm to test if the answer of the student is correct. I'm working in cryptography and i would like to generate question like :

- What is the inverse of a mod p (with a and p randomly generated)

- What is the decomposition in base b of n

- Can you find the cleartext corresponding to this cryptogram : random ciphertext picked from a list of precomputed ciphertexts ...

and so on ...

The two first points can be done with STACK, the last one cannot ...

Re: Question with a random variable

I have to admit, that's a pretty good use case.

David Bowes "scenario" extension would certainly solve your problem. I'm warming to the idea.

Richard

Re: Question with a random variable

Hi,
i tried to contact David Bowed but i had no answer. Do you know where i can download this "forked" version of CodeRunner ?
Thanks.

Re: Question with a random variable

Dear all.

As Richard says, I have forked coderunner to allow scenario generation (and persistence of scenario and student data at the point when the question was attempted)

In order for the fork to work, you have to:

1) use a less restrictive (allows each task to use more memory) version of jobe: https://github.com/comqdhb/jobe.git

2) use the forked version of coderunner: https://github.com/comqdhb/moodle-qtype_coderunner.git (requires dos2unix to also be installed)

3) when creating the coderunner question, tick the box for using twig AND customisation, then  expand the ADVANCED CUSTOMISATION section  and add code to generate scenario data e.g.

<?php

//NB requires php_task.php in jobe to not use --no-php-ini and increase memory to 20000000

$s="{{ SCENARIO.json | e('c') }}";//this is where twig will insert the json of any feeder scenarios$scenario=json_decode($s);//try to convert the json into an object if (!isset($scenario->data)){// json may not have been valid

$scenario->data=new stdClass(); } date_default_timezone_set('UTC'); //stuff to stop php complaining$scenario->data->now=date(DATE_RFC2822);//the current date and time

srand(); //randomise the random number generator

$scenario->data->mon=rand(3,20);//nb a random number from 3 to 20$scenario->required=array();//specify that this needs no values in order to generate values

$scenario->provides=array('mon','date');//specify that this will generate the 'mon' and date variables$scenario->err_message="";//everything ok

print json_encode($scenario);// send the json back The above example scenario generator may appear over complicated. I have a long term use case which is that the scenario from one question will may be used as the starting point for the scenario data of the current question attempt. This will allow for questions to build on each other and have a common scenario. It therefore assumes that prior scenario data is populated in$s

$scenario->required indicates any prior data that must be provided for this generator to produce new data$scenario->provides indicates what data will be generated if it is provided with suitable data.

Why the level of complication? Well, it allows the quiz to decide which questions can be created and which are dependent on other scenarios.  As you can see in the example, required is empty and will therefore always generate data.

Additionally, you may notice that jobe has a Dockerfile, which will allow you to build a jobe image which can then be used to build a docker container.

What i really want to know is how to intercept the quiz building, so that the scenario data can be built at the start of the quiz, with scenario data being passed to all question attempts.  ANY HELP TIM?

David

Re: Question with a random variable

For anyone who lands up here on a web search: the stock CodeRunner now has its own less sophisticated but relatively lightweight question randomisation capability. See here for details.

Richard

Re: Question with a random variable

Great, that's a good news ! i'm going to try to test it and will give you feedback.
Thanks.
Re: Question with a random variable

Hello Pascal,

Just to let you know, I have a plugin for such a problem. It's at https://moodle.org/mod/forum/discuss.php?d=373315

I am investigating coderunner for testing programs, but for generating non programming exercises with random variables, my approach should be easier. Although still experimental, the plugin does not require a server and is quite lightweight.