vendredi 6 novembre 2020

Why does this random character generator print file paths?

I am learning Hash Tables, and I am attempting to create a random word generator to dynamically create key/value pairs. My first pass only prints one string of characters up to the size of the Hash Table.

There has been some unexpected behavior which I haven't been able to puzzle out yet. When I run the program, it mostly outputs a random string of characters. But every few runs, it also prints a full file path from my computer.

For example, this is the typical output:

./hashtable
175dbydtjxbltallnkqlrxutgnhmdellzupavsvepyskydympoxhotcuhjhmouynqnplhmsylkllnjzezxnpsqmzbvo 

However, every so often I get this:

./hashtable dmoqgrzhluqpptpjfgqryvyrigtjfvvihlbpcayowofliwupengckgvtnoesjbcsmdirgifcxmqhkmwozfsmlohyelsqmxiacsrkbynamfjwrfmtmffavoybbtrpscpuuhfxhuzuzissqflcmschgdkhycwqeomaxtyepxaoitjyauamnfvtkfdijzapnoqmkqrzprqxmzvmtyyiftepbhzkibarogram Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/ProgramData/Microsoft/Windows/Start Menu/Programs/Java/bin:/mnt/c/Program Files (x86)/GnuPG/bin:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn/:/mnt/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/170/Tools/Binn/:/mnt/c/Strawberry/c/bin:/mnt/c/Strawberry/perl/site/bin:/mnt/c/Strawberry/perl/bin:/mnt/c/Program Files

This is the code:

#define TABLE_SIZE 1000

int main(int argc, char const *argv[])
{
    char *word;
    int random_number;

    srand(time (NULL));

    int rand_length = rand() % TABLE_SIZE;
    printf("%d", rand_length);
    
    for (size_t i = 0; i < rand_length; i++)
    {
        random_number = rand() % 26;
        char rand_char = 'a' + random_number;
        word[i] = rand_char;
    }
    printf("%s", word); 

    return 0;
}

Any explanations for why this is happening?

UPDATE: When I remove srand this behavior goes away. But I still would like to understand why this is happening.




Aucun commentaire:

Enregistrer un commentaire