Well done on your input file switcheroo - very cunning.
I don't think I really understand what you're after with this random question. How does the student code get hold of the numbers A and B? If it's a pure 'write a program' question, they'd have to read them from a file or standard input.
I attach a question that seems to fulfil the above spec, but I'm at all sure if it's what you're after. Did you want each student to see different random numbers in their tests? That's possible, but it does complicate things and doesn't make the question any more robust against cheating - if one student copies another's code, it will still work in their variant.
I built the question without filling in the 'Expected' fields of the tests, ran the sample answer with Validate On Save on, and just clicked the buttons to copy the expected answers in.
I doubt it's what you want, but it's the starting point for a discussion.