User Tools

Site Tools


random_number_generation

Random Number Generation


The PHP language includes a reasonably good random number generator function called MT_Rand(), which can be used as the basis of random numbers we can use for general programs, such as games and random numbers for math quizzes, etc.

Below is a simple custom randomizing function that may prove useful. It applies the MT_Rand() function and returns an arbitrary-precision string of any number of random digits in any given base from 2 to 36.

It can be used to generate random integer strings or decimal digit strings of any length.

/*
   ========================================================================
   This function generates a string of any number of
   random digits in any given base from 2 to 36.

   All letters (A to Z) used as digits will be returned in uppercase.

   ERRORS:
   On error, FALSE is returned for invalid argument(s).
   ========================================================================
*/

   function Random_Digits_BaseB ($NumDigits=1, $BaseB=10, $LZeroSuppressed=FALSE)
{

// Read arguments.
   $N = intval($NumDigits);
   $B = intval($BaseB);

// Check for invalid argument(s).
   if ($B < 2 or $B > 36 or $N < 1) {return FALSE;}

// Define digits spectrum for the given base.
   $DigitsSpectrum = substr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",0,$B);

// Construct an N-digit random sequence in base B, one digit at
// a time, using a random 50/50 digit alternation method. There
// will be at least one random base B digit returned.
   $RandomDigits = "";

   for ($i=0;   $i < $N;   $i++)
  {
   if (mt_rand() % 2 == 0)
  {$RandomDigits .= substr($DigitsSpectrum, mt_rand(0,$B-1),1);}
   else
  {$RandomDigits = substr($DigitsSpectrum, mt_rand(0,$B-1),1) . $RandomDigits;}
  }

// If $LZeroSuppressed flag === TRUE, then leftmost digit of returned
// random digits string must NOT be zero.  If it is, then another
// random non-zero digit will be attached to the left of it and
// the rightmost digit dropped.  The result will be a normal
// integer value with the required number of digits, but
// never starting with 0 (zero) as the first digit.
   if ($LZeroSuppressed === TRUE)
  {
   $RandomDigits = substr($DigitsSpectrum, mt_rand(1,$B-1),1) . $RandomDigits;
  }
   return substr($RandomDigits, 0, $N);

} // End of  Random_Digits_BaseB(...)

Below is an example program above, showing a few usages of the random digits generator function. Refreshing the page generates new examples.

Example 1:

For a random string of 54 random digits in base 9:

$RandDigitsString = Random_Digits_BaseB (54, 9); 
=
672412428135553000066550557122410784631410847038540446 

-----------------------------------------------------------------------
Example 2:

If the random digits string  is to be interpreted as an integer value, 
then it could possibly begin with a zero (0).  For example, the random
digits, 001, could possibly be generated.

If you want to prevent the leftmost digit from ever coming out as 
zero, then call the function with, LZeroSuppressed = TRUE, as
the last parameter.  Default value = FALSE.  This means that
the minimum integer value can only be number beginning with 1.

For a random string of 47 digits in base 33, with left zero
suppression, the flag set to TRUE prevents a zero from occurring as
the leftmost or first digit in the string.:

$RandDigitsString = Random_Digits_BaseB (47, 33, TRUE); 
=
NT70G8JGI2V0TBBW42O515MIP4N6D7ANDLB3RUBOKCFW7CB 

-----------------------------------------------------------------------
Example 3:

If you want a strictly decimal value that, regardless of base, begins
with '0.', then simply attach the string '0.' to the beginning of the 
random digits string.

For a random string of 91 random base 18 decimal digits:
$RandFraction = '0.' . Random_Digits_BaseB (91, 18);
=
0.AGCCA40GBED6DBEF644C5HF11EG81GA503E55243CGG5B376A9A4EF7H15DCEA38B63C1A048CGF0D5H0F519B35CDF

Below is complete listing of the example program above.

<?php

// Random digits generator examples.

// Generate random numbers for examples.

   $Bx0 = mt_rand(2, 36);
   $digitsx0 = mt_rand(2, 100);
   $RandDigitsString0 = Random_Digits_BaseB ($digitsx0, $Bx0, TRUE);

   $Bx1 = mt_rand(2, 36);
   $digitsx1 = mt_rand(2, 100);
   $RandDigitsString = Random_Digits_BaseB ($digitsx1, $Bx1);
   
   $Bx2 = mt_rand(2, 36);
   $digitsx2 = mt_rand(2, 100);
   $RandInteger = Random_Digits_BaseB ($digitsx2, $Bx2);
   
   $Bx3 = mt_rand(2, 36);
   $digitsx3 = mt_rand(2, 100);
   $RandFraction = "0." . Random_Digits_BaseB ($digitsx3, $Bx3);

// -----------------------------------------------------------------------

print 
"<pre>
Example 1:

For a random string of $digitsx1 random digits in base $Bx1:

&#36;RandDigitsString = Random_Digits_BaseB ($digitsx1, $Bx1); 
=
$RandDigitsString 

-----------------------------------------------------------------------
Example 2:

If the random digits string  is to be interpreted as an integer value, 
then it could possibly begin with a zero (0).  For example, the random
digits, 001, could possibly be generated.

If you want to prevent the leftmost digit from ever coming out as 
zero, then call the function with, LZeroSuppressed = TRUE, as
the last parameter.  Default value = FALSE.  This means that
the minimum integer value can only be number beginning with 1.

For a random string of $digitsx0 digits in base $Bx0, with left zero
suppression, the flag set to TRUE prevents a zero from occurring as
the leftmost or first digit in the string.:

&#36;RandDigitsString = Random_Digits_BaseB ($digitsx0, $Bx0, TRUE); 
=
$RandDigitsString0 

-----------------------------------------------------------------------
Example 3:

If you want a strictly decimal value that, regardless of base, begins
with '0.', then simply attach the string '0.' to the beginning of the 
random digits string.

For a random string of $digitsx3 random base $Bx3 decimal digits:
&#36;RandFraction = '0.' . Random_Digits_BaseB ($digitsx3, $Bx3);
=
$RandFraction

</pre>";


// ==============================================================================
   function Random_Digits_BaseB ($NumDigits=1, $BaseB=10, $LZeroSuppressed=FALSE)
{

// Read arguments.
   $N = intval($NumDigits);
   $B = intval($BaseB);

// Check for invalid argument(s).
   if ($B < 2 or $B > 36 or $N < 1) {return FALSE;}

// Define digits spectrum for the given base.
   $DigitsSpectrum = substr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",0,$B);

// Construct an N-digit random sequence in base B, one digit at
// a time, using a random 50/50 digit alternation method. There
// will be at least one random base B digit returned.
   $RandomDigits = "";

   for ($i=0;   $i < $N;   $i++)
  {
   if (mt_rand() % 2 == 0)
  {$RandomDigits .= substr($DigitsSpectrum, mt_rand(0,$B-1),1);}
   else
  {$RandomDigits = substr($DigitsSpectrum, mt_rand(0,$B-1),1) . $RandomDigits;}
  }

// If LZeroSuppressed flag === TRUE, then leftmost digit of returned
// random digits string must NOT be zero.  If it is, then another
// random non-zero digit will be attached to the left of it and
// the rightmost digit dropped.  The result will be a normal
// integer value with the required number of digits, but
// never starting with 0 (zero) as the first digit.
   if ($LZeroSuppressed === TRUE)
  {
   $RandomDigits = substr($DigitsSpectrum, mt_rand(1,$B-1),1) . $RandomDigits;
  }
   return substr("$RandomDigits", 0, $N);

} // End of  Random_Digits_BaseB(...)  


?>
random_number_generation.txt · Last modified: 2014/04/27 20:43 by Jay.Tanner.x@PHPScienceLabs.us