samedi 13 avril 2019

Printing ASCII alphabet randomly in C

I want to create a random letter by taking the time right now. The algorithm is OK, but there's an error somewhere. Can you help?

Code:

char randomly;
time_t timeNow;
timeNow = time(NULL);
int random;
while (true){
    random = (timeNow*2134+51) % 123;
    printf("\ntest1\n");
    if((random >= 65 && random <= 90) || (random >= 97 && random <= 122)){
        printf("\ntest2\n");
        printf("%d",random);
    }
    break;
}
randomly = (char) random;
printf("\ntest3\n");
printf("%c",randomly);

Out: (changing characters every time I compile)

./bin/program.exe

test1

test3
random : ╗

Out: (one more time)

./bin/program.exe

test1

test3
random : Ï

I want to this

./bin/program.exe

test1

test3
random : a or b or c ... (alphabet)




Aucun commentaire:

Enregistrer un commentaire