/*
============================================================================
This function simply returns a string of random digits in any given base
from 2 to 36.
It makes use of the new Random_Int() function of PHP 7+
ARGUMENTS
NumDigits = Number of digits to generate (at least 1).
Default = 1
Base = Base of number system used for randomized digits (2 to 36).
Default = 10
FALSE is returned if either argument is non-numeric.
NO DEPENDENCIES
============================================================================
*/
function Random_Digits ($NumDigits=1, $Base=10)
{
$N = trim($NumDigits);
$B = trim($Base);
// Error if either argument is non-numeric.
if (!Is_Numeric($N) or !Is_Numeric($B))
{
return FALSE;
}
// Set to minimum of 1 digit if N < 1.
if ($N < 1) {$N = 1;}
// Truncate both arguments
// to positive integers.
$N = floor(abs($N));
$B = floor(abs($B));
// Error if base is out of legal range from 2 to 36.
if ($B < 2 or $B > 36) {return FALSE;}
// Define digits used for bases 2 to 36.
$DIGITS = substr('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 0, $B);
// Execute loop to generate the randomized digits.
$DigitsString = '';
for($n=0; $n < $N; $n++)
{
$DigitsString .= substr($DIGITS, Random_Int(0, $B-1), 1);
}
return "$DigitsString";
} // End of Random_Digits(...)