Is there a way to check answers using rand in MATLAB Grader?

I am using MATLAB Grader to check students' work. Many basic problems ask them to create particular arrays using rand or randi. Is there a way to check if their answer is a possible correct answer?
For example, a problem could ask them to generate a vector v1 of 20 random integers between -4 and 2.
v1 = randi([-4,2],1, 20);
But if I use the assessVariableEqual function to check their answer, it won't work because their answer contains randomized values. I'd like to be able to check the min, max, and length of the result to see that it is within the expected range. Is there a way to do that?

4 Comments

How about asking students to use their roll-number as a seed for rng and checking the values correspondingly.
Though that might defeat the pupose of the use of randi().
You can fix the seed for the random number generator. The numbers generated will be the same if you use the same seed.
See the accepted answer for a better solution.
Sunil's solution also works fine. The reason my checker wasn't working before was that I have my students run rng('shuffle') at the top of any program than uses random number functions. But I can tell students not to use rng('shuffle'), or run an assessFunctionAbsence command for rng, and then use the following:
rng(SEEDNUMBER) apparently this line is unncessary because the grader automatically sets the seed the same.
assessVariableEqual('v1', referenceVariables.v1);

Sign in to comment.

 Accepted Answer

Random numbers can be treated the same as other variables. Reference and Learner solutions share the same seed, meaning they will generate the same random numbers. Check the variables the same as has been done in problems that do not use random numbers.
You can see an example of how to design problems that use random numbers in the Getting Started with MATLAB Grader collection > Coordinate Transformations - Navigating a robot problem.

6 Comments

Ok this is helpful, thanks. This would allow me to check a simple problem like the one I provided in the example.
The problem I see is that, if the student uses the random number generator a different number of times, or in a different order, than the reference code, they will still have different values.
Are there other functions (beyond the 3 given in Grader) for checking students' answers?
That is correct. Both Workspaces are launched with the same seed, but in order to work, their solution must call rand the same number of times and in the same order as the reference solution.
Unless creating these variables is critical to the learning objective, you may consider providing your learners with those particular lines of code as part of the Learner Template. You can even lock the lines so students cannot accidentally change or remove them.
MATLAB Grader provides 3 assessment functions, which can be accessed either from the Assessment Test Type dropdown menu, or programmatically in a MATLAB Code Assessment Test Type.
Assessment tests are considered correct unless the code returns an error. One common way to write your own MATLAB Code assessement test is to use assert statements (see this answer). While there are no other Grader-specific helper functions, you have access to the functions in MATLAB to design your own assessment code.
This is super helpful – thanks! What I've figured out so far is that I can use the following code the check.
assert(length(v1) == 20, 'The vector is not the right length')
One comment - assessVariableEqual (and it's dropdown equivalent) actually perform 4 tests. They are
  1. Variable exists
  2. Variable is the same data type as the reference variable
  3. Variable is the same size as the reference variable
  4. Variable values are the same as the reference variable.
So a test on variable length/size may not be necessary, as that is already part of the checks built into assessVariableEqual.
I wouldn't assert on the length here. I'd either assert on the size of the variable or assert on the vector-ness and the number of elements it has. If the input isn't a vector, simply checking length may give a misleading result.
A = magic(20);
assert(length(A) == 20, "The vector(?) is not the right length") % passes
x = 1:20;
assert(isvector(x), "The x variable is not a vector")
assert(numel(x) == 20, "The x variable does not have 20 elements")
assert(isequal(size(x), [1 20]), "The x vector is not a 1-by-20 vector")
If you want to specifically check for a row vector or a column vector, use isrow or iscolumn instead of isvector (which accepts both.)
isvector(x)
ans = logical
1
isvector(x.')
ans = logical
1
isrow(x) % true
ans = logical
1
isrow(x.') % false, x.' is a column vector
ans = logical
0
You could probably even use some of the functions intended for validating function arguments. I'm not sure if these names would be easier to understand than calling assert, but they might be. They also provide standardized messages when their conditions are not satisfied.
x = randi([4, 20], 1, 10)
x = 1×10
11 11 18 8 7 6 11 5 19 16
mustBeInRange(x, 4, 10) % oops! Upper bound should have been 20 not 10
Value must be greater than or equal to 4, and less than or equal to 10.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!