Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file RandomUtil.class.php

Documentation is available at RandomUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: RandomUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  */
  12.  
  13. /**
  14.  * RandomUtil
  15.  *
  16.  * @package Zikula_Core
  17.  * @subpackage RandomUtil
  18.  */
  19. class RandomUtil
  20. {
  21.     /**
  22.      * Return a seed value for the srand() function
  23.      *
  24.      * @return The resulting seed value
  25.      */
  26.     function getSeed ()
  27.     {
  28.         $factor 95717// prime
  29.         list($usec$secexplode(" "microtime());
  30.         return (double) strrev(($usec)*$factor/M_PI);
  31.     }
  32.  
  33.  
  34.     /**
  35.      * Return a random integer between $floor and $ceil (inclusive)
  36.      *
  37.      * @param floor     The lower bound
  38.      * @param ceil      The upper bound
  39.      * @param seed      Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  40.      *
  41.      * @return The resulting random integer
  42.      */
  43.     function getInteger ($floor$ceil$seed=false)
  44.     {
  45.         if ($seed{
  46.             srand(RandomUtil::getSeed());
  47.         }
  48.  
  49.         $diff $ceil $floor;
  50.  
  51.         // mr_rand seems to sometimes generate idential
  52.         // series of random numbers. rand seems to do better.
  53.         //$inc  = mt_rand (0, $diff);
  54.         $inc  rand (0$diff);
  55.  
  56.         return $floor $inc;
  57.     }
  58.  
  59.  
  60.     /**
  61.      * Return a random string of specified length. This function uses
  62.      * uses md5() to generate the string.
  63.      *
  64.      * @param length    The length of string to generate
  65.      * @param seed      Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  66.      *
  67.      * @return The resulting random integer
  68.      */
  69.     function getRandomString ($length$seed=true)
  70.     {
  71.         $res '';
  72.  
  73.         if ($seed{
  74.             srand(RandomUtil::getSeed());
  75.         }
  76.  
  77.         while (strlen($res$length{
  78.             $res .= md5(RandomUtil::getInteger (0100000));
  79.  
  80.         }
  81.  
  82.         return substr ($res0$length);
  83.     }
  84.  
  85.  
  86.     /**
  87.      * Return a random string
  88.      *
  89.      * @param minLen    The minimum string length
  90.      * @param maxLen    The maximum string length
  91.      * @param leadingCapital Whether or not the string should start with a capital letter (optional) (default=true)
  92.      * @param useUpper       Whether or not to also use uppercase letters (optional) (default=true)
  93.      * @param useLower       Whether or not to also use lowercase letters (optional) (default=true)
  94.      * @param useSpace       Whether or not to also use whitespace letters (optional) (default=true)
  95.      * @param useNumber      Whether or not to also use numeric characters (optional) (default=false)
  96.      * @param useSpecial     Whether or not to also use special characters (optional) (default=false)
  97.      * @param seed           Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  98.      * @param dontuse        Array of characters not to use (optional) (default=null) eg $dontuse=array('a', 'b', 'c');
  99.      *
  100.      * @return The resulting random string
  101.      */
  102.     function getString ($minLen$maxLen$leadingCapital=true$useUpper=true$useLower=true,
  103.     $useSpace=false$useNumber=false$useSpecial=false$seed=false$dontuse=null)
  104.     {
  105.         $rnd     '';
  106.         $chars   '';
  107.         $upper   "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  108.         $lower   "abcdefghijklmnopqrstuvwxyz";
  109.         $number  "0123456789";
  110.         $special "~@#$%^*()_+-={}|][";
  111.  
  112.         if ($seed{
  113.             srand(RandomUtil::getSeed());
  114.         }
  115.  
  116.         if ($useLower{
  117.             $chars .= $lower;
  118.         }
  119.  
  120.         if ($useUpper{
  121.             $chars .= $upper;
  122.         }
  123.  
  124.         if ($useNumber{
  125.             $chars .= $number;
  126.         }
  127.  
  128.         if ($useSpecial{
  129.             $chars .= $special;
  130.         }
  131.  
  132.         if ($useSpace{
  133.             for ($i=0$i<(strlen($chars)%10)$i++{
  134.                 $chars .= ' ';
  135.             }
  136.         }
  137.  
  138.         // omit the following characters
  139.         if (!is_null($dontuse&& is_array($dontuse)) {
  140.             $chars str_replace($dontuse''$chars);
  141.         }
  142.  
  143.         $len RandomUtil::getInteger ($minLen$maxLen);
  144.         $clen strlen($chars1;
  145.         for ($i=0$i<$len$i++{
  146.             $rnd .= $chars[(RandomUtil::getInteger(0,$clen))];
  147.         }
  148.  
  149.         if ($leadingCapital{
  150.             $rnd ucfirst($rnd);
  151.         }
  152.  
  153.         return $rnd;
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Return a random sentence of nWords based on the dictionary
  159.      *
  160.      * @param nWords     The number of words to put in the sentence
  161.      * @param dictArray  The array of dictionary words to use
  162.      * @param seed       Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  163.      *
  164.      * @return The resulting random date string
  165.      */
  166.     function getSentence ($nWords$dictArray$seed=false)
  167.     {
  168.         if (!$nWords{
  169.             return pn_exit ('Invalid nWords passed to RandomUtil::getLanguageSentence ...');
  170.         }
  171.  
  172.         if (!$dictArray{
  173.             return pn_exit ('Invalid dictArray passed to RandomUtil::getLanguageSentence ...');
  174.         }
  175.  
  176.         if ($seed{
  177.             srand(RandomUtil::getSeed());
  178.         }
  179.  
  180.         //$dictArray = explode (' ', $dict);
  181.         $nDictWords count ($dictArray);
  182.         $txt '';
  183.  
  184.         $t '';
  185.         for ($i=0$i<$nWords$i++)
  186.         {
  187.             $rnd RandomUtil::getInteger (0$nDictWords);
  188.             $word $dictArray[$rnd];
  189.  
  190.             if ($i == 0{
  191.                 $word ucfirst ($word);
  192.             else {
  193.                 $word strtolower ($word);
  194.             }
  195.  
  196.             if (RandomUtil::getInteger(010)==&& $i<$nWords-&& !strpos($word,','&& !strpos($word,'.')) {
  197.                 $word .= ', ';
  198.             }
  199.  
  200.             if (strpos($word,'.'!== false{
  201.                 $word substr($word0-1);
  202.             }
  203.  
  204.             $t .= "$word ";
  205.         }
  206.  
  207.         $txt .= substr($t0-1'. ';
  208.         return $txt;
  209.     }
  210.  
  211.  
  212.     /**
  213.      * Return a nParas paragraphs of random text based on the dictionary
  214.      *
  215.      * @param nParas         The number of paragraphs to return to put in the sentence
  216.      * @param dict           The dictionary to use (a space separated list of words)
  217.      * @param irndS          The number of sentences in a paragraph (optional) (default=0=randomlyGenerated)
  218.      * @param irndW          The number of words in a sentence (optional) (default=0=randomlyGenerated)
  219.      * @param startCustomary Whether or not to start with the customary phrase (optional) (default=false)
  220.      * @param seed           Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  221.      *
  222.      * @return The resulting random date string
  223.      */
  224.     function getParagraphs ($nParas$dict=''$irndS=0$irndW=0$startCustomary=false$seed=false)
  225.     {
  226.         if (!$nParas{
  227.             return pn_exit ('Invalid nParas passed to RandomUtil::getLanguageParagraphs ...');
  228.         }
  229.  
  230.         if (!$dict{
  231.             return pn_exit ('Invalid dict passed to RandomUtil::getLanguageParagraphs ...');
  232.         }
  233.  
  234.         if ($seed{
  235.             srand(RandomUtil::getSeed());
  236.         }
  237.  
  238.         $dictArray explode (' '$dict);
  239.         $txt '';
  240.         for ($i=0$i<$nParas$i++{
  241.             if (!$irndS{
  242.                 $rndS RandomUtil::getInteger (37);
  243.             else {
  244.                 $rndS $irndS;
  245.             }
  246.  
  247.             for ($j=0$j<$rndS$j++{
  248.                 if (!$irndW{
  249.                     $rndW RandomUtil::getInteger (825);
  250.                 else {
  251.                     $rndW $irndW;
  252.                 }
  253.                 $txt .= RandomUtil::getSentence ($rndW$dictArray);
  254.             }
  255.             $txt .= "\n";
  256.         }
  257.  
  258.         // start with first 5 words
  259.         if ($startCustomary{
  260.             $pre '';
  261.             for ($i=0$i<5$i++{
  262.                 $pre .= $dictArray[$i' ';
  263.             }
  264.             $startLetter substr ($txt01);
  265.             $txt $pre strtolower($startLettersubstr($txt1);
  266.         }
  267.  
  268.         return $txt;
  269.     }
  270.  
  271.  
  272.     /**
  273.      * Return a random date between $startDate and $endDate
  274.      *
  275.      * @param startDate  The lower date bound
  276.      * @param endDate    The high date bound
  277.      * @param format     The date format to use
  278.      * @param seed       Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  279.      *
  280.      * @return The resulting random date string
  281.      */
  282.     function getDate ($startDate$endDate$format=DATEFORMAT_FIXED$seed=false)
  283.     {
  284.         if ($seed){
  285.             srand(RandomUtil::getSeed());
  286.         }
  287.  
  288.         $t1 strtotime($startDate);
  289.         $t2 strtotime($endDate);
  290.  
  291.         $diff $t2 $t1;
  292.         $inc  RandomUtil::getInteger(0$diff);
  293.  
  294.         $tRand $t1 $inc;
  295.         Loader::loadClass ('DateUtil');
  296.         return DateUtil::getDatetime ($tRand$format);
  297.     }
  298.  
  299.  
  300.     /**
  301.      * Return a random user-id
  302.      *
  303.      * @param seed      Whether or not to seed the random number generator (optional) (default=false) seeding not required for PHP>4.2.0
  304.      *
  305.      * @return The resulting random user-id
  306.      */
  307.     function getUserID ($seed=false)
  308.     {
  309.         if ($seed{
  310.             srand(RandomUtil::getSeed());
  311.         }
  312.  
  313.         $fa  DBUtil::selectFieldArray ('users''uid');
  314.         $pos RandomUtil::getInteger(0count($fa));
  315.         return $fa[$pos];
  316.     }
  317.  
  318.  
  319. }

Documentation generated on Fri, 18 Jul 2008 21:56:35 +0200 by phpDocumentor 1.4.1