使用m个可能的字符生成字符串列表(n长度)

I need to generate a String List with N-Length and M possible chars. Currently I'm using PHP/XAMPP.

My function works great for generating the strings with 62 possible chars (a-z, A-Z, 0-9) and up to the length of 4 chars. However, when I want to generate longer strings I'm running out of memory.

I've set the memory limit, but I'm still running out of memory.

ini_set('memory_limit', '-1');

the error I get:

Fatal error:  Out of memory (allocated 1858600960) (tried to allocate 36 bytes) in C:\xampp\htdocs\index.php on line 51

how can I solve this problem? Should I switch to another language for more performance when generating the string list?

EDIT, the code I'm working with:

http://pastebin.com/f6pA6Ra0

are you trying to get a random string with 5 length with M as possible characters?

then I propose taking a random element of M 5 times, and not making an array of every single possibility (which is 916'132'832 possibilities (62^5) with this array of chars and length of 5 !)

here is a non-recursive function that returns a random string where you can define the size.

function sampling($size){
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $output = '';
    $charLength = strlen($chars) - 1;
    for ($i = 0; $i < $size; $i++) {
        $n = rand(0, $charLength);
        $output .= $chars[$n];
    }
    return $output; 
}

if I misunderstood your intentions and you really want to get all possibilities I am sorry ;)