vendredi 24 février 2017

C++ random number between different ranges (ASCII)

So I asked this question here: Random number between more ranges (ascii) and it was marked duplicate, but they are two different questions and the guy that duped it didn't undupe it, so here I go again.

I created a random password(geslo) generator that is between 6 and 10 characters long, using the ASCII table. So far I only made it so that the password contains only numbers(ASCII values from 48 to 57) but I need it to also generate small and big characters randomly (ranges from 65 to 90 and from 97 to 122). I tried to think it through mathematically and I just can't seem to figure it out. Here is what I have:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
    for(int i=0; i<10; i++){


        int x = rand() % 4 + 6;

        string geslo;

        for(int j=0;j<x; j++){

            int y = rand() % 9 + 48;
            char q = (char) y;
            geslo+=q;
        }

        cout << "geslo " << i+1 << ": " << geslo << endl;
    }
    return 0;
}

Also the int x just makes it so that the password length is between 6 and 10.




Aucun commentaire:

Enregistrer un commentaire