C code that will select a random word from an array and include it in a string.

This code will pick a random word from the x[] array and include it in the printed string. This program will also get your current Linux username to customize the output. I am sure this code would be very useful to someone who wants to know how to handle arrays in C.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int cool(void) {
	int i;
	int k;
 
	i = 15;
	k = 0;
	srand((unsigned)time(NULL));
	k = rand() % i;
	return k;
}
 
const char* x[] = {
	"Baron of Hell", "Demon", "Hellknight",
	"Cyberdemon", "Mancubus", "Revenant",
	"Doomimp", "Zombieman", "Shotguy",
	"Beholder", "Moloch", "Satyr",
	"Afrit", "Cybermastermind", "Nightmarecacodemon",
};
 
int main() {
	char *player;
	player = getenv("LOGNAME");
 
	printf("%s was slaughtered by a %s.\n",player , x[cool()]);
 
	return 0;
}

No comments have been made. Use this form to start the conversation :)

Leave a Reply