Skip to content

Commit

Permalink
ENH Include filename of script in predictable_random_int()
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Feb 14, 2024
1 parent 1eaf525 commit bd84b27
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 11 additions & 3 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,23 @@ 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);
preg_match("#$dirQuoted/(.+)$#", $callingFile, $matches);
$relativePath = $matches[1];
var_dump("$MODULE_DIR-$relativePath");
$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 show as "eval()'d code" for the calling file in debug_backtrace()
$ret = null;
eval('$ret = predictable_random_int(15);');
$this->assertSame(2, $ret);
}
}

0 comments on commit bd84b27

Please sign in to comment.