Words Me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 10000
#define MAX_LEN 100
#define PICK_COUNT 36 // word count
unsigned int secure_rand(unsigned int max) {
unsigned int r;
FILE *f = fopen("/dev/urandom", "rb");
if (!f) {
perror("urandom");
exit(1);
}
fread(&r, sizeof(r), 1, f);
fclose(f);
return r % max;
}
int main(void) {
FILE *f = fopen("words.txt", "r");
if (!f) {
perror("words.txt");
return 1;
}
char words[MAX_WORDS][MAX_LEN];
int count = 0;
while (count < MAX_WORDS && fgets(words[count], MAX_LEN, f)) {
words[count][strcspn(words[count], "\n")] = '\0';
count++;
}
fclose(f);
if (count == 0) {
fprintf(stderr, "No words found\n");
return 1;
}
for (int i = 0; i < PICK_COUNT; i++) {
int idx = secure_rand(count);
printf("%s", words[idx]);
if ((i + 1) % 36 == 0) {
putchar('\n'); // newline after every XX words
} else {
putchar(' '); // space between words
}
}
return 0;
}
INFO