canabatz Posted December 17, 2009 Share Posted December 17, 2009 hi' how can i randomize the letters a,b,c,d,e all possible ways, and i want to print the result and unique from each? thanx Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2009 Share Posted December 17, 2009 echo implode(" ", (shuffle(range('a','z')))); edit: missing ) Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted December 17, 2009 Share Posted December 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2009 Share Posted December 17, 2009 Sorry, forgot shuffle doesn't actually return an array. $arr = range('a','z'); shuffle($arr); echo implode(" ", $arr); Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 do u want to randomly display the letters from a-z or else do u want something like all possible combination s of the letters a-z Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 17, 2009 Author Share Posted December 17, 2009 this is working : <? $arr = array('a','b','c','d','e'); shuffle($arr); echo implode(" ", $arr); ?> how do i display all possible combinations? thanx!!! Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2009 Share Posted December 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 @thrope i know that it will give only 1000 combinations, i just gave it as an example.. he need to use the permutations to calculate the number of possible methods.. Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 17, 2009 Author Share Posted December 17, 2009 i think i just need to know to possible combinations possible with a,b,c,d,e ,and put it in the for loop and if it will be unique ,then it will be the target Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 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.. Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 17, 2009 Author Share Posted December 17, 2009 sorry ym but i get only this as result: 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 thnx Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2009 Share Posted December 17, 2009 Theres a small typo. Call the function with either.... 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')); or pc_permute(range('a','z')); Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 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.. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 this function will take the first two letters a b and shuffle them in possible fashion and then a b c like this it will continue... Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 17, 2009 Author Share Posted December 17, 2009 thanx guys for your help!! Quote Link to comment Share on other sites More sharing options...
printf Posted December 17, 2009 Share Posted December 17, 2009 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 ) Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 17, 2009 Share Posted December 17, 2009 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.