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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

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

Post 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
Last edited by tecktalkcm0391 on Tue May 30, 2006 1:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)];
}
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks, It works great!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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'
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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; 

?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Its not working, so I guess not! Can anyone help me with this?
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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; 

?>
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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; 

?>
Post Reply