--- /home/noobsauce/D/New_Zealand/Employment/Catalyst/Moodle_Bug_Test/restore_dbops.class.php	2018-10-05 15:25:32.855906110 +1300
+++ restore_dbops.class.php	2018-10-07 22:34:04.443635866 +1300
@@ -679,6 +679,9 @@
                                                                   FROM {question}
                                                                  WHERE category = ?", array($matchcat->id));
 
+                    // Temporarily log the stamp and version of random questions when they are created.
+		    $randomqcache = array();
+                    
                     foreach ($questions as $question) {
                         if (isset($questioncache[$question->stamp." ".$question->version])) {
                             $matchqid = $questioncache[$question->stamp." ".$question->version];
@@ -711,7 +714,16 @@
 
                         // 5b) Random questions must always be newly created.
                         } else if ($question->qtype == 'random') {
-                            // Nothing to mark, newitemid means create
+                            if (in_array($matchqid, $randomqcache)) {
+                               // Random question was already created.
+                               // Match, mark q to be mapped.
+                               self::set_backup_ids_record($restoreid, 'question', $question->id, $matchqid);
+
+                            } else {
+                               $randomqcache[] = $matchqid;
+                               // Nothing to mark, newitemid means create
+                             }
+
 
                         // 5c) Match, mark q to be mapped.
                         } else {
