Question Authors' Forum

Question with a random variable

Picture of David Bowes
Re: Question with a random variable
by David Bowes - Monday, 3 April 2017, 8:16 PM
Dear all.

Appearing out of teaching overload..... 

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:

2) use the forked version of coderunner: (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.


//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?


Picture of Richard Lobb
Re: Question with a random variable
by Richard Lobb - Saturday, 28 April 2018, 9:26 PM

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.


Picture of Pascal Veron
Re: Question with a random variable
by Pascal Veron - Wednesday, 2 May 2018, 9:16 PM
Great, that's a good news ! i'm going to try to test it and will give you feedback.
Picture of C Daly
Re: Question with a random variable
by C Daly - Sunday, 15 July 2018, 5:40 AM

Hello Pascal,

Just to let you know, I have a plugin for such a problem. It's at

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.