I need a function to generate a random alphanumeric password in C code. It needs to be between 6-8 characters and follow the following rules:
Reject if same char appears # time: 4 or more
Reject if same char appears consecutively: 3 or more
I have the following random password working for numbers (0-9). I need to update it to include characters (a-z). I also need to add the validation rules above. Can someone please help me. I have been trying for days to update it but get a lot of compile errors... Thanks..
Here's what is working for number (0-9):
Random_Password()
{
int ran_int_num;
time_t seconds;
time(&seconds);
srand ((unsigned int) seconds);
/* multiply by 975 to generate a larger number */
ran_int_num = rand() * 975;
/**************************************************/
/* Need to cast the integer to character, so use sprintf /
/ %u = unsigned decimal integer %d = signed decimal integer /
/ ************************************************/
sprintf(randompw, "%u", ran_int_num);
strncpy(newpw, randompw, 8);
return(0);
}
Here's what I tried to change it to, in order to generate an alphanumeric:
GenerateRandomID()
{
char password_chars["1234567890abcdefghijklmnopqrstuvwxyz"];
int i;
int length;
char ticketid;
int random;
for \(i = 0; i < length; i\+\+\) \{
random = rand\(0, strlen\(password_chars\) - 1\);
ticketid = password_chars[random];
\}
return ticketid;
}