Question Authors' Forum

Python Random Module Questions

Python Random Module Questions

by Levi Osborne -
Number of replies: 4

My student are getting ready to use the Random module in python. I'm having trouble imagining how to grade their code in coderunner, as the expected output will be random and won't match the student's code most of the time.

For example, if I have them write code to simulate a roll of two dice, they will import random and then print(random.randint(2,12).

But the expected output is sometimes 2, sometimes 8, sometimes 3, etc. etc. 

Any thoughts on how to set up this question better?

In reply to Levi Osborne

Re: Python Random Module Questions

by Richard Lobb -

This thread may help. Essentially the idea is to set the random number generator seed before the student's code gets called. If your question spec prescribes an exact procedure to be followed, and you've set the random number seed to a known value, the result is then known, too.

To prevent students simply printing a single known expected answer you will need multiple tests, each with a different seed. 

More complex situations, in which  (for example) you use a different random number seed for each student, require use of a template grader.


In reply to Richard Lobb

Re: Python Random Module Questions

by Levi Osborne -

I saw that thread and it makes a lot of sense. However, my students don't learn functions until next week (I know, I know, modules before functions seems like putting the cart before the horse, but I wanted them to work with some functions from modules before they started to write their own!).

I'm a little stumped as to how to set the seed and test it if the student is just writing a general program instead of writing a function I can test? 

Would I just have to have the students set the seed to 0 and then do the same in my answer for grading? Or is there a way to run their program with a set seed so that I get the same results regardless?

Thank you for the reply and the help! Coderunner has been amazing and has really improved my ability to teach Python. I very much appreciate it!

In reply to Levi Osborne

Re: Python Random Module Questions

by Richard Lobb -

You could put a statement that sets the seed in the Extra template data field of each test case (a different seed each time). Then, use a per-test template (i.e. by unchecking the Is combinator checkbox) like

{{ TEST.extra}}
{{ STUDENT_ANSWER }}

This has the slight downside that any errors in the students code will have out-by-one line numbers, but students usually seem to get by. [It's possible to correct line numbers by post-processing but that's way harder.]

In reply to Richard Lobb

Re: Python Random Module Questions

by Antonio Gortan -

You can also leave the combinator check box checked if necessary, but then you'll have to introduce for loop as:

from random import seed

{% for TEST in TESTCASES %}
{{ TEST.extra }}
{% endfor %}

{{ STUDENT_ANSWER }}

This has worked for me.