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

Posted: January 21, 2015. At: 12:26 PM. This was 3 years ago. Post ID: 8023
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

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;
	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