'Defines a custom response problem that uses a random value to determine correctness.
Generated answer is also returned as the `msg`, so that the value can be used as a
correct answer by a test.
If the `redefine` flag is set, then change the definition of correctness (from equals
to not-equals).'

def define_randomized_custom_response_problem(self, problem_url_name, redefine=False):

    factory = CustomResponseXMLFactory()
    script = textwrap.dedent(("\n                                                                def    check_func(expect,    answer_given):\n                                                                                expected    =    str(random.randint(0,    100))\n                                                                                return    {'ok':    answer_given    %s    expected,    'msg':    expected}\n                                                " % ('!=' if redefine else '==')))
    problem_xml = factory.build_xml(script=script, cfn='check_func', expect='42', num_responses=1)
    if redefine:
        descriptor = self.module_store.get_item(InstructorTaskModuleTestCase.problem_location(problem_url_name))
        descriptor.data = problem_xml
        with self.module_store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, descriptor.location.course_key):
            self.module_store.update_item(descriptor, self.user.id)
            self.module_store.publish(descriptor.location, self.user.id)
    else:
        ItemFactory.create(parent_location=self.problem_section.location, category='problem', display_name=str(problem_url_name), data=problem_xml, metadata={'rerandomize': 'per_student'})
