Updated Random String generator CakePHP component

Here is a new component that will generate random strings for you. You can pass it a set of characters to pick from and how long your random string should be. I use it to generate passwords but you can do just about anything with it.

To use it, create a file called random_helper.php in app/controllers/components and copy the following code.

class RandomHelperComponent extends Object {
 * Random string generator function
 * This function will randomly generate a password from a given set of characters
 * @param int = 8, length of the password you want to generate
 * @param string = 0123456789abcdefghijklmnopqrstuvwxyz all possible values
 * @return string, the password
	function generateRandomString ($length = 8, $possible = '0123456789abcdefghijklmnopqrstuvwxyz') {
		// initialize variables
		$password = "";
		$i = 0;
		// add random characters to $password until $length is reached
		while ($i < $length) {
			// pick a random character from the possible ones
			$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
			// we don't want this character if it's already in the password
			if (!strstr($password, $char)) { 
				$password .= $char;
		return $password;

make sure you include it in the appcontroller as such

var $components = array('Auth','Email','RandomHelper');

then you can call it as follows in any function

$password= $this->RandomHelper->generateRandomString(4,'abcdefghijklmnopqrstuvwxyz');