The two lines
$this->catform_rand->set_data($category);
$this->catform_rand->display();
Should not be present. To see why, go to Question Bank management -> Categories tab -> Click on the edit icon next to a category.
You end up on a page with two forms, when only the top one should be present.
By the way, the purpose of the code
if (1 == $DB->count_records_sql("SELECT count
FROM
{question_categories}
c2
WHERE c2.id = ?
AND c1.contextid = c2.contextid", array($currentcat)))
Well, the SQL is counting the number of other categories belonging to the same context as this one. If there is only one, then it is trying to stop you changing the parent (so you can't remove the default category from any context.) However, that should not apply when you are creating a new context. (That applies to the standard create category form too.) Can you either fix this, or create a new issues assigned to me.
//TODO: Tim: is this questionable? to add a hidden field right from a get parameter?
$mform->addElement('hidden', 'addonpage', optional_param("addonpage_form", 0, PARAM_SEQUENCE), 'id="rform_qpage"');
Yes, that is questionable. You should do the optional_param call on the page that loads the form, and pass the data in via the call to set_data. Hmm that is tricky, because the call to set_data is in the initialize method. It might be better to call set_data in display_randomquestion_user_interface(), and pass $addonpage as an argument to that.