Skip to content

Commit

Permalink
Merge pull request #39 from creative-commoners/pulls/main/random-int
Browse files Browse the repository at this point in the history
ENH Include filename of script in predictable_random_int()
  • Loading branch information
GuySartorelli authored Feb 15, 2024
2 parents 1eaf525 + 7cb4fe3 commit 7a6baed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
17 changes: 14 additions & 3 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,26 @@ function human_cron(string $cron): string
}

/**
* Creates a predicatable random int between 0 and $max based on the module name to be used with the % mod operator.
* Creates a predicatable random int between 0 and $max based on the module name and file name script
* is called with to be used with the % mod operator.
* $offset variable will offset both the min (0) and $max. e.g. $offset of 1 with a max of 27 will return an int
* between 1 and 28
* Note that this will return the exact same value every time it is called for a given module.
* Note that this will return the exact same value every time it is called for a given filename in a given module
*/
function predictable_random_int($max, $offset = 0): int
{
global $MODULE_DIR;
$chars = str_split($MODULE_DIR);
$callingFile = debug_backtrace()[0]['file'];
// remove absolute path e.g. /home/myuser/...
$moduleStandardiserDir = basename(__DIR__);
$dirQuoted = preg_quote($moduleStandardiserDir);
// double $dirQuoted is for github actions CI where there which will have a directory strcture
// with /module-standardiser/module-standardiser/...
if (!preg_match("#/$dirQuoted/$dirQuoted/(.+)$#", $callingFile, $matches)) {
preg_match("#/$dirQuoted/(.+)$#", $callingFile, $matches);
}
$relativePath = $matches[1];
$chars = str_split("$MODULE_DIR-$relativePath");
$codes = array_map(fn($c) => ord($c), $chars);
mt_srand(array_sum($codes));
return mt_rand(0, $max) + $offset;
Expand Down
22 changes: 22 additions & 0 deletions tests/FuncsScriptsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use PHPUnit\Framework\TestCase;

class FuncsScriptsTest extends TestCase
{
public function testPredictableRandomInt()
{
global $MODULE_DIR;
$MODULE_DIR = 'lorem';
$this->assertSame(0, predictable_random_int(15));
$this->assertSame(25, predictable_random_int(30));
$this->assertSame(45, predictable_random_int(30, 20));
$MODULE_DIR = 'donuts';
$this->assertSame(13, predictable_random_int(15));
// use eval to simulate calling from a different file
// it will suffix "(19) : eval()'d code" to the calling file in debug_backtrace()
$ret = null;
eval('$ret = predictable_random_int(15);');
$this->assertSame(2, $ret);
}
}

0 comments on commit 7a6baed

Please sign in to comment.