Jump to content

generating all possible random letters


canabatz

Recommended Posts

Without testing it, i dunno, but try:

$array('a','b','c','d','e');
$count=count($array);
$i=1;
while($i<=$count){
   $item=rand($array);
   echo $item;
   $array=(array()-$item)
   $i++;
}

 

I'm not sure how you'd get that to work... possibly use explode on the array as a string or set the array using $1='a'; etc, then remove them.

 

you could do if statements to say

if($item='a'){$array=X

Possibly a switch statement might work better.

 

I thought I'd post this anyway, but Thorpe's post's much better.

this is working :

<? 
$arr = array('a','b','c','d','e');
shuffle($arr);
echo implode(" ", $arr);
?>

 

 

 

 

how do i display all possible combinations?

 

thanx!!!

 

for this as thrope has always mentioned it out..

u can use that

but u need to loop it out for the calculating the number of possible permutations..

 

i have modified his code to generate the 1000 combination s

<?php

for ($i=1;$i<=1000;$i++)
{
$arr = range('a','z');
shuffle($arr);
echo implode(" ", $arr);echo "<br>";
}
?>

this is working :

<? 
$arr = array('a','b','c','d','e');
shuffle($arr);
echo implode(" ", $arr);
?>

 

 

 

 

how do i display all possible combinations?

 

thanx!!!

 

for this as thrope has always mentioned it out..

u can use that

but u need to loop it out for the calculating the number of possible permutations..

 

i have modified his code to generate the 1000 combination s

<?php

for ($i=1;$i<=1000;$i++)
{
$arr = range('a','z');
shuffle($arr);
echo implode(" ", $arr);echo "<br>";
}
?>

 

That will just create 1000x26 random letters, not all combinations of letters.

 

To the OP, do you really want all combinations? That'll take some work and will likely be pretty tough on your server.

i think this would resolve it out..

<?php
function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print join(' ', $perms) . "\n";
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);echo "<br>";
             array_unshift($newperms, $foo);
             pc_permute($newitems, $newperms);
         }
    }
}

pc_permute(split('  ', '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'));

?>

 

but one thing try it in ur localhost.. 8)

 

Funny  ;) but wht went wrong is that in this line

pc_permute(split('  ', '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'));

 

give only one blank space in the

' '

 

like this

pc_permute(split(' ', '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'));

 

if there are dual blank space it will stop after abc--z output.. :P

If you want the combination to be unique, I would do something like this...

 

 

<?php

function combinations ( $a )
{
$b = array ();
$c = sizeof ( $a );
$d = pow ( 2, $c );

for ( $e = 0; $e <= $d; $e++ )
{
	$f = '';
	$g = 1;
	$h = 1;

	for ( $i = 1; $i < $c; $i++ )
	{
		$g = $g * 2;
		$h = $h + $g;
	}

	$j = $e;

	for ( $k = $g; $k >= 1; $k = $k / 2 )
	{
		$l = ( $j / $k ) >= 1 ? 1 : 0;

		if ( $l == 1 )
		{
			$j = $j - $k;
		}

		$f .= $l;
	}

	$m = '';

	for ( $n = 0; $n <= $c; $n++ )
	{
		$m .= $f{$n} == '1' ? $a[$n] : '';
	}

	$b[] = $m;
}

return array_slice ($b, 1, - 1 );
}

$array = array ( 'a', 'b', 'c' );

$all = combinations ( $array );

print_r ( $all );

?>

 

Which returns... these unique combination

 


Array
(
    [0] => c
    [1] => b
    [2] => bc
    [3] => a
    [4] => ac
    [5] => ab
    [6] => abc
)

If you want the combination to be unique, I would do something like this...

 

 

<?php

function combinations ( $a )
{
   $b = array ();
   $c = sizeof ( $a );
   $d = pow ( 2, $c );

   for ( $e = 0; $e <= $d; $e++ )
   {
      $f = '';
      $g = 1;
      $h = 1;

      for ( $i = 1; $i < $c; $i++ )
      {
         $g = $g * 2;
         $h = $h + $g;
      }

      $j = $e;

      for ( $k = $g; $k >= 1; $k = $k / 2 )
      {
         $l = ( $j / $k ) >= 1 ? 1 : 0;

         if ( $l == 1 )
         {
            $j = $j - $k;
         }

         $f .= $l;
      }

      $m = '';

      for ( $n = 0; $n <= $c; $n++ )
      {
         $m .= $f{$n} == '1' ? $a[$n] : '';
      }

      $b[] = $m;
   }

   return array_slice ($b, 1, - 1 );
}

$array = array ( 'a', 'b', 'c' );

$all = combinations ( $array );

print_r ( $all );

?>

 

Which returns... these unique combination

 


Array
(
    [0] => c
    [1] => b
    [2] => bc
    [3] => a
    [4] => ac
    [5] => ab
    [6] => abc
)

 

of course the function which i gave will also give the unique values... :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.