Sunyanzi
2013-03-26 13:39:03 +08:00
<?php
$letters = range( 'a', 'z' );
$big_array = [];
$generator = function( $str = '', $depth = 0 )
use ( &$generator, &$big_array, $letters ) {
++ $depth;
if ( 7 === $depth ) {
$big_array[] = $str;
return;
}
foreach( $letters as $letter )
$generator( $str . $letter, $depth );
};
$generator();
shuffle( $big_array );
$ret = array_slice( $big_array, 0, 1000000 );
用随机洗牌的思路给你简要写了一个 ... 初步判断生成完毕大概要花几个小时的样子 ...
这个只是为了演示说办法可行 ... 更好的解决方案如下 ...
<?php
$letters = range( 'a', 'z' );
$generator = function() use ( $letters ) {
$ret = '';
for ( $i = 0; $i < 6; ++ $i )
$ret .= $letters[mt_rand( 0, 25 )];
return $ret;
};
$buffer = [];
for ( $i = 0; $i < 1000000; ++ $i ) {
$current = $generator();
if ( ! isset( $buffer[$current] ) )
$buffer[$current] = 0;
else -- $i;
}
$ret = array_keys( $buffer );
生成一百万的话应该也要十几分钟 ...