Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions assessment/macros/randomizers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
'rands',
'rrands',
'randfrom',
'randkeyfrom',
'randsfrom',
'randkeysfrom',
'jointrandfrom',
'diffrandsfrom',
'nonzerorand',
Expand Down Expand Up @@ -138,6 +140,23 @@ function randfrom($lst) {
}


function randkeyfrom($lst) {
if (func_num_args() != 1) {
echo "randkeyfrom expects 1 argument";
return 1;
}
if (!is_array($lst)) {
echo "randkeyfrom expects an array";
return 1;
}
if (count($lst) == 0) {
echo 'cannot pick randkeyfrom empty array';
return '';
}
return $GLOBALS['RND']->array_rand($lst, 1);
}


function randsfrom($lst, $n, $ord = 'def') {
if (func_num_args() < 2) {
echo "randsfrom expects 2 arguments";
Expand All @@ -163,6 +182,27 @@ function randsfrom($lst, $n, $ord = 'def') {
}


function randkeysfrom($lst, $n = 1) {
if (func_num_args() < 1) {
echo "randkeysfrom expects at least 1 argument";
return 1;
}
if (!is_array($lst)) {
echo "randkeysfrom expects an array";
return 1;
}
$n = floor($n);
if ($n <= 0) {
echo "randkeysfrom: need n &gt; 0";
}
if (count($lst) == 0) {
echo 'cannot pick randkeysfrom empty array';
return '';
}
return $GLOBALS['RND']->array_rand($lst, $n);
}


function jointrandfrom() {
$args = func_get_args();
if (count($args) < 2) {
Expand Down
5 changes: 5 additions & 0 deletions help.html
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,8 @@ <h2 id="randomizers">Randomizers</h2>

$arr = [10,20,30]
$val = randfrom($arr) // might return 20</pre></details></li>
<li><strong>randkeyfrom(array)</strong>: Return a random key from the array.
<details class="example"><summary>Example</summary><pre>$x = randkeyfrom(["a"=>"A", "b"=>"B", "c"=>"C"]) // e.g. $x="b"</pre></details></li>
<li><strong>randname(),randmalename(),randfemalename()</strong>: Returns a random first name
<details class="example"><summary>Example</summary><pre>$name = randname() // might return "Sofia"
$name = randmalename() // might return "Carlos"
Expand Down Expand Up @@ -2369,6 +2371,9 @@ <h2 id="randomizers">Randomizers</h2>
<details class="example"><summary>Example</summary><pre>$a,$b = randsfrom("red,green,blue,yellow", 2) // e.g. "blue", "red"
$arr = [2,4,6,8]
$a,$b = randsfrom($arr, 2, 'inc'); // sorted ascending, e.g. 2, 6</pre></details></li>
<li><strong>randkeysfrom(array,n)</strong>: Return n random keys from the array.
<details class="example"><summary>Example</summary><pre>$arr = [0=>'x_0', 1=>'x_1', 2=>'x_2', 3=>'x_3']
$a,$b = randkeysfrom($arr, 2); // e.g., $a=2, $b=0</pre></details></li>
<li><strong>jointrandfrom(list/array,list/array,[list/array,...])</strong>: Returns one element from each list, where the location used in each list is the same
<details class="example"><summary>Example</summary><pre>$first,$last = jointrandfrom("Alice,Bob,Carlos", "Smith,Jones,Rivera")
// picks the same index from each list, so names stay paired:
Expand Down