I'm a student learning C I am trying to write a random string generator for my program, everything runs fine and when I execute the program it prints a random string of letters and numbers but when executed again it prints the same string again.
Code Below:
#include <stdio.h>
const int max=5;
char *randstring(size_t);
main()
{
char *test;
test=randstring(max);
printf("%s",test);
free(test);
}
char *randstring(size_t length) {
static char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789";
char *randomString = NULL;
if (length)
{
randomString=malloc(sizeof(char)*(length +1));
if (randomString)
{
for (int n = 0;n<length;n++)
{
int key = rand()%(36-1);
randomString[n] = charset[key];
}
}
}
return (char *)randomString;
}
any help, suggestions, or comments are appreciated. Thank you