Page 1 of 2

Random Characters: Still can't get it to work Chr(rand());

Posted: Tue May 30, 2006 11:23 am
by tecktalkcm0391
Ok I have this so far:

Code: Select all

<?php
$len = 6

    $code = "";
    for($i = 1; $i <= $len; ++$i) {
      $code .= chr(rand(65, 90));
    }
    return $code;

?>
I don't want it just to be chr(65,90) i want it to be 48-50 and 65-90, can anyone help ?

The character numbers match to ASCII table (http://www.lookuptables.com/)

Thanks

Posted: Tue May 30, 2006 11:32 am
by feyd

Code: Select all

$code = '';
$pool = range(48, 50) + range(65, 90);
$pool = implode('', array_map('chr', $pool));
$size = strlen($pool) - 1;
for($i = 0; $i < $len; ++$i)
{
  $code .= $pool[mt_rand(0, $size)];
}

Posted: Tue May 30, 2006 11:37 am
by tecktalkcm0391
Thanks, It works great!

Posted: Tue May 30, 2006 12:41 pm
by tecktalkcm0391
Now I am having a problem. My code is:

Code: Select all

<?php

	$code = ''; 
	$pool = range(50, 57) + range(65, 72) + range(74, 75) + range(77, 78) + range(80, 80) + range(82, 90) ; 
	$pool = implode('', array_map('chr', $pool)); 
	$size = strlen($pool) - 1; 
	for($i = 0; $i < $len; ++$i) 
	{ 
	  $code .= $pool[mt_rand(0, $size)]; 
	} 
		return $code;

?>
Everytime I run this to get a code most of the time it only show numbers, and the sometimes its just numbers and Z's how come only numbers are comming up?

Posted: Tue May 30, 2006 12:53 pm
by feyd
the pool you've built is just numbers and 'z'.

Code: Select all

[feyd@home]>php -r "$pool = range(50, 57) + range(65, 72) + range(74, 75) + range(77, 78) + range(80, 80) + range(82, 90); $pool = implode('', array_map('chr', $pool)); var_export($pool);"
'23456789Z'

Posted: Tue May 30, 2006 12:58 pm
by tecktalkcm0391
How could I fix it to include only these characters on the ASCII Table (http://www.lookuptables.com/):

50-57
65-72
74
75
77
78
80
82-90

Posted: Tue May 30, 2006 1:00 pm
by feyd

Posted: Tue May 30, 2006 1:01 pm
by tecktalkcm0391
Can you explain this., I am pretty new to PHP.

If you could give me some type of code, I might understand it more.

Posted: Tue May 30, 2006 1:13 pm
by feyd
instead of adding all the arrays returned from range() together, use array_merge() to combine them. I'm not going to write the code for this as you should be able to figure it out just fine.

Posted: Tue May 30, 2006 1:17 pm
by tecktalkcm0391
is this right?

Code: Select all

<?php

                $code = ''; 
	$characters1 = array(50, 57);
                $characters2 = array(65-72 );
                $pool = array_merge($characters1, $characters2);
	$pool = implode('', array_map('chr', $pool)); 
	$size = strlen($pool) - 1; 
	for($i = 0; $i < $len; ++$i) 
	{ 
	  $code .= $pool[mt_rand(0, $size)]; 
	} 
		return $code; 

?>

Posted: Tue May 30, 2006 1:28 pm
by tecktalkcm0391
Its not working, so I guess not! Can anyone help me with this?

Posted: Tue May 30, 2006 1:51 pm
by tomprogers
I don't know if it's important to your application to include non-printing characters, but this is something I've used with great success for generating random strings, and it has the added benefit of not requiring you to deal with arrays.

Code: Select all

$source = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for($i = 0; $i < $length; $i++)
	$sOut .= $source{rand(0, strlen($source) - 1)};
return $sOut;
On a related note: you will need to sit down and figure the array functions out at some point. There are some tasks that PHP can only reasonably perform with arrays.

Posted: Tue May 30, 2006 1:53 pm
by tecktalkcm0391
Can someone just give me an example of the array, and can someone tell me why this isn't working.

Code: Select all

<?php 

                $code = ''; 
        $characters1 = array(50, 57); 
                $characters2 = array(65-72 ); 
                $pool = array_merge($characters1, $characters2); 
        $pool = implode('', array_map('chr', $pool)); 
        $size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)]; 
        } 
                return $code; 

?>

Posted: Tue May 30, 2006 2:01 pm
by tomprogers
I think your problem is related to the fact that array(65-72) will create an array with a single element, the value of which is equal to 65 minus 72. feyd was using the range() function, which returns the array you're looking for.

This isn't particularly elegant, but you could just do this:

Code: Select all

$codes = array_merge(range(A-B), range(C-D), ...);
$pool = implode('', array_map('chr', $codes));
for($i = 0; $i < $len; $i++)
   $str .= $pool[mt_rand(0, count($pool)];
return $str;
You will, of course, want to replace A, B, C, and D with the endpoints of the appropriate ranges, and you will have one array_merge argument for each range you're interested in.

Posted: Tue May 30, 2006 8:59 pm
by tecktalkcm0391
Would this be right?

Code: Select all

<?php 

        $code = ''; 
        $characters = array_merge(range(2-9), range(A-H), range(J-K), range(M-N), range(R-Z) ); 
        $pool = implode('', array_map('chr', $characters)); 
        $size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)]; 
        } 
                return $code; 

?>