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 Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/ 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 ) Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979060 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. Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979061 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); Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979062 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 Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979064 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!!! Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979067 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979069 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. Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979070 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.. Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979071 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 Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979072 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.. Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979076 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 Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979086 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')); Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979087 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.. Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979089 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... Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979093 Share on other sites More sharing options...
canabatz Posted December 17, 2009 Author Share Posted December 17, 2009 thanx guys for your help!! Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979108 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 ) Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979187 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... Link to comment https://forums.phpfreaks.com/topic/185446-generating-all-possible-random-letters/#findComment-979192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.