Random string generator I decided to make a random string generator for fun, and it works for the most part. Since I'm an amateur at programming, I thought I would post the code to get some feedback on it, and hopefully improve any bad habits.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;ctime&gt;

typedef unsigned int uint;

std::string randomString(uint l = 15, std::string charIndex = "abcdefghijklmnaoqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
{ 
    // l and charIndex can be customized, but I've also initialized them.

    uint length = rand() % l + 1; 
    // length of the string is a random value that can be up to 'l' characters.

    uint ri[15]; 
    /* array of random values that will be used to iterate through random 
      indexes of 'charIndex' */

    for (uint i = 0; i &lt; length; ++i) 
        ri[i] = rand() % charIndex.length();
    // assigns a random number to each index of "ri"

    std::string rs = ""; 
    // random string that will be returned by this function

    for (uint i = 0; i &lt; length; ++i) 
        rs += charIndex[ri[i]];
    // appends a random amount of random characters to "rs"

    if (rs.empty()) randomString(l, charIndex); 
    // if the outcome is empty, then redo the generation (this doesn't work, help?)
    else return rs;
}

int main()
{
    srand(time(NULL));
    std::cout &lt;&lt; randomString() &lt;&lt; std::endl;
}
Your comments are too long (line length).
Your code is also also seems untidy because of them. I would reduce the comments and have use self documenting code.

This seems to short. What happens if l is 2048?

uint ri[15];


I would use a std::vector&lt;uint&gt; here. You can initalize it with length so that it will be the correct size.

You do the iteration twice. Once to generate the random numbers. The second time to use the random number to access your set of characters. Why not do this in a single loop.

Here is your bug:

    if (rs.empty()) randomString(l, charIndex);
    else return rs;


Can't see it! Add braces. and use more vertical space. You should use {} around all sub blocks. It will help avoid issues in the long run.

    if (rs.empty()) {
        randomString(l, charIndex);
    }
    else {
        return rs;
    }


Do you see it now? You forgot the return in the first branch of the if statement. You only return a value in the else section. This means your class has undefined behavior.