/ VairuX Systems (http://www.vairux.com) Name: LuckyStrike Version: 1.1 License: BSD License / http://opensource.org/licenses/bsd-license.php Language: PHP 4 Description: LuckyStrike is a class that provides randomization functions and strings analisys. With LuckyStrike functions you can make numerologic calculations, extract Consonant-Vowel patterns from words, generate new words based on Consonant-Vowel patterns, pick random choices based on user-input and more. Functions available are: randWord() randPattern() randAnswer() intelligentRandAnswer() intelligentRandWord() intelligent() seedInRange() analyzePattern() numerologic() VairuX Systems - http://www.vairux.com */ class LuckyStrike { // This class provides functions related to random results // depending on some data sets. // This class was written by Braian Gómez for VairuX Systems. // this is an intensive work with random things and human interaction. // Returns a random word based on $pattern (Consonant-Vowel Pattern) function randWord($pattern = null, $cArray = null, $vArray = null){ /* Pattern format: * C = uppercase Consonant * c = lowercase Consonant * V = uppercase Vowel * v = lowercase Vowel * 0 = even number * 1 = odd number * # = any number. */ $output = null; if ($cArray == null) { // Default consonant array (no ñ, special chars or ch, ll). $cArray = array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'); } if ($vArray == null) { // Default Vowel array (no accents, no special chars) $vArray = array('a','e','i','o','u'); } if ($pattern == null) { $pattern = $this->randPattern(array('c','v')); } $len = strlen($pattern); for ($i = 0; $i < $len; $i++) { switch($pattern[$i]) { case "v": $output .= $this->randAnswer($vArray); break; case "V": $output .= strtoupper($this->randAnswer($vArray)); break; case "c": $output .= $this->randAnswer($cArray); break; case "C": $output .= strtoupper($this->randAnswer($cArray)); break; case "0": $randNum = mt_rand(0,9); if ($randNum % 2 == 1) { // It's odd, convert to even. $randNum--; } $output .= $randNum; break; case "1": $randNum = mt_rand(0,9); if ($randNum % 2 == 0) { // It's even, convert to odd. $randNum++; } $output .= $randNum; break; case "#": $output .= mt_rand(0,9); break; case "/": $output .= $pattern[$i+1]; $i++; break; default: $output .= $pattern[$i]; break; } } return $output; } // Returns a random string based on items in $patterValues (array) and $size. If $size is not provided, it's calculated randomly. function randPattern($patternValues, $size = null){ // Makes a new pattern based on values / size. Returns a random pattern string. $output = null; if ($size == null) { $size = mt_rand(); } for($i = 0; $i<$size; $i++) { $ptt = mt_rand(0,count($patternValues)); $output += $patternValues[$ptt]; } return $output; } // Returns a random item from $answersArray function randAnswer($answersArray){ $randNum = mt_rand(0, count($answersArray)-1); return $answersArray[$randNum]; } // Returns a semi-random item from $answersArray based on $question function intelligentRandAnswer($question, $answersArray){ /* * Just uses the intelligent() function to generate an answer * depending on an input and random value */ //$numb = $this->seedInRange($this->numerologic($question)+mt_rand(), 0, count($answersArray)-1); $numb = $this->seedInRange($this->intelligent($question)+mt_rand(), 0, count($answersArray)-1); //$numb = mt_rand(0, $this->intelligent($question,0, count($answersArray))); return $answersArray[$numb]; } // Returns a semi-random word based on $question and $pattern (Consonant-Vowel Pattern) function intelligentRandWord($question, $pattern = null, $cArray = null, $vArray = null){ /* Same as randWord but with the option of asking a question */ /* Pattern format: * C = uppercase Consonant * c = lowercase Consonant * V = uppercase Vowel * v = lowercase Vowel * 0 = even number * 1 = odd number * # = any number. */ $output = null; if ($cArray == null) { // Default consonant array (no ñ, special chars or ch, ll). $cArray = array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'); } if ($vArray == null) { // Default Vowel array (no accents, no special chars) $vArray = array('a','e','i','o','u'); } if ($pattern == null) { $pattern = $this->randPattern(array('c','v')); } $len = strlen($pattern); for ($i = 0; $i <= $len; $i++) { switch($pattern[$i]) { case "v": $output .= $this->intelligentRandAnswer($question, $vArray); break; case "V": $output .= strtoupper($this->intelligentRandAnswer($question, $vArray)); break; case "c": $output .= $this->intelligentRandAnswer($question, $cArray); break; case "C": $output .= strtoupper($this->intelligentRandAnswer($question, $cArray)); break; case "0": $randNum = $this->seedInRange($this->intelligent($question)+mt_rand(),0,9); if ($randNum % 2 == 1) { // It's odd, convert to even. $randNum--; } $output .= $randNum; break; case "1": $randNum = $this->seedInRange($this->intelligent($question)+mt_rand(),0,9); if ($randNum % 2 == 0) { // It's even, convert to odd. $randNum++; } $output .= $randNum; break; case "#": $output .= $this->seedInRange($this->intelligent($question)+mt_rand(),0,9); break; case "/": $output .= $pattern[$i+1]; $i++; break; default: $output .= $pattern[$i]; break; } } return $output; } // Returns a number depending on $string. function intelligent($string, $min=null, $max=null) { /* * Returns a number depending on string. * Just adds all characters values in ascii of the string. * In combination to a random number, adds a little * of "free will" to the random answer. */ $result = 0; $string = strtoupper($string); $len = strlen($string); for ($i=0; $i<$len; $i++) { $result = $result+(ord($string[$i])); } if ($min == null AND $max == null) { return $result; } else { return $this->seedInRange($result, $min, $max); } } /* Old "seedInRange" function. It's here to remind how dumb could be the human being. function dumbrange($seed, $min, $max) { $step = $min; for($i = $min; $i < $seed; $i++) { $step++; if ($step==$max) { $step = $min; } } return $step; } */ // Takes $seed and tries to fit in $min and $max range. function seedInRange($seed, $min, $max) { // This is the optimization for dumbrange function. // It just takes $seed and tries to fit in $min and $max range. // NOTE THAT: This function just works for INTEGER values, and min needs to be lower than max. return ($seed%(($max+1)-$min)) + $min; } // Returns a pattern string (with "C" for consonants and "V" for vowels) based on $word input. // Allows customization of consonant ($cArray) and vowel ($vArray) arrays. function analyzePattern($word, $cArray = null, $vArray = null) { // This isn't a lucky function but it helps to analyze // the pattern of a given word (or sentence) $output = null; if ($cArray == null) { // Default consonant array (no ñ, special chars or ch, ll). $cArray = array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'); } if ($vArray == null) { // Default Vowel array (no accents, no special chars) $vArray = array('a','e','i','o','u'); } $len = strlen($word); for ($i = 0; $i <= $len; $i++) { if (in_array(strtolower($word[$i]), $vArray)){ // it's vowel $output .= "v"; } else if (in_array(strtolower($word[$i]), $cArray)) { //it's consonant $output .="c"; } else { //it's NONE $output .= $word[$i]; } } return $output; } // Returns a number (0-9) based on a numerologic analisys of the $input. function numerologic($input){ $output = null; $len = strlen($input); for ($i=0; $i<$len; $i++) { $charVal = ord(strtoupper($input[$i]))-64; if (is_numeric($input[$i]) AND $input[$i] > 0) { // For Numbers $output += $input[$i]; } else if ($charVal > 0 AND $charVal < 27) { // For Characters $output += $charVal; } } return $this->seedInRange($output-1, 1, 9); } } ?>