How to generate random uppercase characters in C

2 Answers

0 votes
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h> 
 
int main(void) {
    srand((unsigned int) time(NULL) + getpid());
 
    int len = 10, total = 5;
    while (total--) {
        int tmp = len;
        while (tmp--) {
            putchar('A' + (rand() % 26));
            srand(rand());
        }
        printf("\n");
        tmp = len;
    }
 
    return 0;
}
  
  
  
/*
run :
  
DZHDMKOYSL
OZTHGNWDHI
LHNGPGFLQW
TNKEQOKFMO
QSYKKCHCEV
  
*/

 



answered Feb 14, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> 
#include <time.h>

int main(void) {
    srand((unsigned int) time(NULL) + getpid());
    
    char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int letterslen = strlen(letters);

    int len = 10, total = 5;
    while (total--) {
        int tmp = len;
        while (tmp--) {
            putchar(letters[rand() % letterslen]);
            srand(rand());
        }
        printf("\n");
        tmp = len;
    }
 
    return 0;
}
  
  
  
/*
run :
  
KDOEWPSKCL
VFUNGTLSIT
VNCCQMGIDV
TNYWBZDHTS
EXCOLPEVME
  
*/

 



answered Feb 14, 2021 by avibootz

Related questions

1 answer 184 views
1 answer 185 views
1 answer 94 views
1 answer 128 views
1 answer 154 views
1 answer 158 views
...