samedi 25 janvier 2020

How can I get a seed for srand() that takes the nanoseconds instead of seconds?

im coding a password generator, but i have to use a delay if i want the output to be diferent, i have the code (a bit messy tho).

//Variables
int menu = 0, prec = 0, digit = 0;
char password;

//Menú
cout << "##############################" << endl;
cout << "#  Generador de Contraseñas  #" << endl;
cout << "#----------------------------#" << endl;
cout << "#            MENÚ            #" << endl;
cout << "#   (1) Generador   -        #" << endl;
cout << "#   (2) Historial   -        #" << endl;
cout << "#   (3) Contraseñas -        #" << endl;
cout << "#       guardadas   -        #" << endl;
cout << "#                            #" << endl;
cout << "##############################" << endl;
cin >> menu;

while (menu == 1)
{
    cout << "¡Bienvenido al generador de contraseñas! Introduce la longitud de la contraseña (entre 8 y 32)" << endl;
    cin >> prec;
    if (prec <= 8 || prec <= 32)
    {
        cout << "Estamos calculando tu contraseña" << endl;
        while (digit++ < prec)
        {
            srand(time(NULL));
            password = (rand() % 74) + 48;
            cout << password;
            Sleep(1000);

        }
        cout << " es la contraseña generada."<< endl;
        digit = 0;
    }
    else
    {
        cout << "Has puesto un numero que no esta entre 8 y 32, introduce otro" << endl;
        cin >> prec;
    }
}

while (menu == 2)
{

}

while (menu == 3)
{

}
system("pause");

}

Im a bit new in c++, so if there is any way to do it better, im open to suggestions. im using windows by the way




Aucun commentaire:

Enregistrer un commentaire