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.

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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

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.