How to pick 2 random characters from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
  
int main(void) {
    char str[] = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-=+]}[{;:'/?.><,|`~";
  
    int size = strlen(str);
  
    srand((unsigned int)time(NULL));
  
    char ch2, ch1 = str[rand() % (size - 1)];
    
    do {
        ch2 = str[rand() % (size - 1)];
    } while (ch1 == ch2);

    printf("%c %c", ch1, ch2);
  
    return 0;
}
  
  
  
/*
run:
    
p <
    
*/

 

 



answered Feb 3, 2024 by avibootz

Related questions

1 answer 172 views
1 answer 171 views
1 answer 188 views
1 answer 165 views
1 answer 139 views
1 answer 139 views
...