mercredi 27 mai 2015

Random password generator same string

This is my first C program and I wanted to make a random password, but every time I run the program, it generates the same string. (always generates "pkDHTxmMR1...") This is not going to actually be used so the security of rand() doesn't really matter to me. Why would it output the same string every time that I run it?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>  
//this is a program to generate a random password

int main()
{
int counter = 0;
srand(time(NULL));
char randChar;

int  passwordLength;

printf("Type in a password Length \n");
scanf("%d", &passwordLength);


while(counter < passwordLength)
{



//seed random based on time
srand(time(NULL));
    randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"[random () % 62];    
    printf("%c", randChar);
    counter++;
}
return 0;

}




Aucun commentaire:

Enregistrer un commentaire