Source for file RandomUtil.class.php
Documentation is available at RandomUtil.class.php
* Zikula Application Framework
* @copyright Robert Gasch
* @link http://www.zikula.org
* @version $Id: RandomUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @author Robert Gasch rgasch@gmail.com
* Return a seed value for the srand() function
* @return The resulting seed value
$factor = 95717; // prime
return (double) strrev(($usec)* $factor/ M_PI);
* Return a random integer between $floor and $ceil (inclusive)
* @param floor The lower bound
* @param ceil The upper bound
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random integer
// mr_rand seems to sometimes generate idential
// series of random numbers. rand seems to do better.
//$inc = mt_rand (0, $diff);
* Return a random string of specified length. This function uses
* uses md5() to generate the string.
* @param length The length of string to generate
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random integer
while (strlen($res) < $length) {
return substr ($res, 0, $length);
* @param minLen The minimum string length
* @param maxLen The maximum string length
* @param leadingCapital Whether or not the string should start with a capital letter (optional) (default=true)
* @param useUpper Whether or not to also use uppercase letters (optional) (default=true)
* @param useLower Whether or not to also use lowercase letters (optional) (default=true)
* @param useSpace Whether or not to also use whitespace letters (optional) (default=true)
* @param useNumber Whether or not to also use numeric characters (optional) (default=false)
* @param useSpecial Whether or not to also use special characters (optional) (default=false)
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @param dontuse Array of characters not to use (optional) (default=null) eg $dontuse=array('a', 'b', 'c');
* @return The resulting random string
function getString ($minLen, $maxLen, $leadingCapital= true, $useUpper= true, $useLower= true,
$useSpace= false, $useNumber= false, $useSpecial= false, $seed= false, $dontuse= null)
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lower = "abcdefghijklmnopqrstuvwxyz";
$special = "~@#$%^*()_+-={}|][";
for ($i= 0; $i< (strlen($chars)% 10); $i++ ) {
// omit the following characters
for ($i= 0; $i< $len; $i++ ) {
* Return a random sentence of nWords based on the dictionary
* @param nWords The number of words to put in the sentence
* @param dictArray The array of dictionary words to use
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random date string
function getSentence ($nWords, $dictArray, $seed= false)
return pn_exit ('Invalid nWords passed to RandomUtil::getLanguageSentence ...');
return pn_exit ('Invalid dictArray passed to RandomUtil::getLanguageSentence ...');
//$dictArray = explode (' ', $dict);
$nDictWords = count ($dictArray);
for ($i= 0; $i< $nWords; $i++ )
$word = $dictArray[$rnd];
if (strpos($word,'.') !== false) {
$txt .= substr($t, 0, - 1) . '. ';
* Return a nParas paragraphs of random text based on the dictionary
* @param nParas The number of paragraphs to return to put in the sentence
* @param dict The dictionary to use (a space separated list of words)
* @param irndS The number of sentences in a paragraph (optional) (default=0=randomlyGenerated)
* @param irndW The number of words in a sentence (optional) (default=0=randomlyGenerated)
* @param startCustomary Whether or not to start with the customary phrase (optional) (default=false)
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random date string
function getParagraphs ($nParas, $dict= '', $irndS= 0, $irndW= 0, $startCustomary= false, $seed= false)
return pn_exit ('Invalid nParas passed to RandomUtil::getLanguageParagraphs ...');
return pn_exit ('Invalid dict passed to RandomUtil::getLanguageParagraphs ...');
for ($i= 0; $i< $nParas; $i++ ) {
for ($j= 0; $j< $rndS; $j++ ) {
// start with first 5 words
$pre .= $dictArray[$i] . ' ';
$startLetter = substr ($txt, 0, 1);
* Return a random date between $startDate and $endDate
* @param startDate The lower date bound
* @param endDate The high date bound
* @param format The date format to use
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random date string
function getDate ($startDate, $endDate, $format= DATEFORMAT_FIXED, $seed= false)
* Return a random user-id
* @param seed Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
* @return The resulting random user-id
|