Skip to content

Add randkeyfrom and randkeysfrom macros and their descriptions in help.php - #466

Open
DaleWMyers wants to merge 1 commit into
drlippman:masterfrom
DaleWMyers:add-randkeyfrom-randkeysfrom
Open

Add randkeyfrom and randkeysfrom macros and their descriptions in help.php#466
DaleWMyers wants to merge 1 commit into
drlippman:masterfrom
DaleWMyers:add-randkeyfrom-randkeysfrom

Conversation

@DaleWMyers

Copy link
Copy Markdown
Contributor

Summary

Adds two new randomizer macros for MyOpenMath: randkeyfrom and randkeysfrom.
These mirror PHP's array_rand(), but pull from MOM's seeded random generator
($GLOBALS['RND']) so results stay reproducible for grading/regrading, consistent
with the rest of the macros in randomizers.php.

New functions

randkeyfrom($array)
Returns a single random key from an array. Equivalent to array_rand($array).

$arr = ["a"=>"A", "b"=>"B", "c"=>"C"];
$x = randkeyfrom($arr);  // e.g. $x = "b"

randkeysfrom($array, $n = 1)
Returns $n random keys from an array. Equivalent to array_rand($array, $n).
Unlike PHP's native array_rand(), which requires [$a, $b] = array_rand(...)
for multiple keys, randkeysfrom returns a plain array so it works with MOM's
existing multi-assignment syntax:

$arr = [0=>'x_0', 1=>'x_1', 2=>'x_2', 3=>'x_3'];
$a,$b = randkeysfrom($arr, 2);   // e.g. $a=2, $b=0
[$a,$b] = randkeysfrom($arr, 2); // e.g. $a=1, $b=3

randkeysfrom only accepts arrays (not comma-separated lists), since lists
have no keys — this differs from randfrom/randsfrom, which accept both.

Files changed

  • assessment/macros/randomizers.php — function definitions, added to allowedmacros
  • help.html — documentation entries added after the existing randfrom/randsfrom entries

Testing

Verified locally against QID 1141.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant