Randomy - Random letter generator

Discussion in 'PHP' started by CodedCaffeine, Jan 2, 2010.

  1. #1
    Here is a PHP CLI script I wrote the other day. I thought it was cool and felt like releasing it. (Fun to run in terminal/cmd prompt)

    Feel free to correct any of my errors in this script. Mainly wrote it to figure out PHP5 OOP a little more.

    Just be sure to change the first line so it matches your php executable.

    #!C:\wamp\bin\php\php5.2.11\php.exe -q
    <?php
    /**
     * Randomy
     *
     * A PHP based script that generates random text until it reaches the
     *   desired username.
     *
     * @package	Randomy
     * @author		Joel Larson
     * @copyright	Copyright (c) 2009 - 2010, Joel Larson.
     * @link		http://thejoellarson.com/
     * @since		Version 1.0
     * @filesource
     */
    
    // ------------------------------------------------------------------------
    
    /**
     * Randomy
     *
     * Allows the random text to be generated based off the username.
     *
     * @package	Randomy
     * @subpackage	AwesomeClasses
     * @category	random
     * @author		Joel Larson
     * @link		
     */
    class Randomy
    {
    	protected $name;
    	protected $param;
    
    	/**
    	 * Randomy::Execute()
    	 *
    	 * Executes the script based on username.
    	 *
    	 * @access	static
    	 * @param	string
    	 * @return	object
    	 */
    	static function execute($name)
    	{
    		return new Randomy($name);
    	}
    	
    	// ------------------------------------------------------------------------
    	
    	/**
    	 * Randomy()
    	 *
    	 * Initilizes the runtime function.
    	 *
    	 * @access	private
    	 * @param	string
    	 * @return	none
    	 */
    	private function __construct($name)
    	{
    		$this->_runtime($name);
    	}
    	
    	// ------------------------------------------------------------------------
    	
    	/**
    	 * Randomy->_runtime()
    	 *
    	 * Runs the Randomy features.
    	 *
    	 * @access	private
    	 * @param	string
    	 * @return	none
    	 */
    	private function _runtime($user)
    	{
    		$this->name = $user;
    		$i = 0;
    		
    		$loop = strlen($this->name)*100;
    		
    		while($i < $loop)
    		{
    			if(($i % 100) == 0)
    				$this->param = $i/100;
    			
    			echo $this->_generate();
    			
    			if(strlen($this->name) == $this->param)
    				break;
    			
    			$i++;
    			usleep(10000);
    		}
    		
    		echo "\n\nThe Winner is: {$this->name}!\n\n";
    	}
    	
    	// ------------------------------------------------------------------------
    
    	/**
    	 * Randomy->_generate()
    	 *
    	 * Generates the random text and username parts.
    	 *
    	 * @access	private
    	 * @return	string
    	 */
    	private function _generate()
    	{
    		$string = substr($this->name, 0, $this->param);
    		$length = strlen($this->name);
    		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    		$total = strlen($chars)-1;
    		$length = $length - strlen($string);
    		$p = 0;
    		while($p < $length)
    		{
    			$string .= $chars[mt_rand(0, $total)];
    			$p++;
    		}
    		return $string."\n";
    	}
    	
    }
    // END OF RANDOMY
    
    # Random array of users.
    $array = array(
    	'CodedCaffeine',
    	'AnotherUser',
    	'YetAnotherUser'
    );
    
    # Pick a random position
    $select = array_rand($array);
    
    # Use this to generate from a username.
    Randomy::execute($array[$select]);
    
    /* END OF FILE */
    PHP:
     
    CodedCaffeine, Jan 2, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nice. Don't forget you can also just run this in a php file without the hasbang at the top of the script
     
    JAY6390, Jan 2, 2010 IP