Jump to content

Generator fail.


President Obama

Recommended Posts

Had a crack at making my own generator which makes some jumble 32 characters long with letters and numbers. In logic it seemed fine to me. It practice it just does nothing.

function generate(){
$abc = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); 
$num = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
$result = ""; 

for ($i = 0; $i < 32; $i++){
	$bool = rand(0,1);
	if ($bool = 1) {
		$result . $abc[rand(0,25)];	
	} else {
		$result . $num[rand(0,9)];
	}
}
echo $result;
}

Link to comment
Share on other sites

<?php
   $length = 32;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters) -1 )];
    }
    echo $string;
?>

Link to comment
Share on other sites

or a function

 

<?php
function generateString($length) {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";    
    for ($s = 0; $s < $length; $s++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

//usage
$randomstring = generateString(32);//define amount of characters
echo $randomstring;
?>

Link to comment
Share on other sites

oops forgot to add the -1 to define

also added more numbers to offset the letters more

 

<?php
function generateString($length) {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";    
    for ($s = 0; $s < $length; $s++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }
    return $string;
}

//usage
$randomstring = generateString(32);//define amount of characters
echo $randomstring;
?>

 

Link to comment
Share on other sites

You can even just pull a random md5 if wanted, and do range for length 1 to 32

 

<?php
function uniqueString($length) {
$string = substr(md5(uniqid()), 0,$length);
return $string;
}
//usage
$random_string = uniqueString(10);//min 1 to max 32 for length
echo $random_string;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.