First of all, your program has the following issues:
- In your posted input, some of the words are 5 characters long, but your array only has room for 4 characters plus the terminating null character.
- The words in your posted input are separated by newline characters, not commas. Therefore, it does not make sense that you are searching the input stream for
','
instead.
After fixing these two issues in your code and adding a function main
and all necessary headers, it should look like this:
#include <stdio.h>
int main( void )
{
FILE* inp;
inp = fopen("wordlist.txt","r"); //filename of your data file
char arr[100][6]; //max word length 5
int i = 0;
while(1) {
char r = (char)fgetc(inp);
int k = 0;
while(r!='\n' && !feof(inp)) { //read till , or EOF
arr[i][k++] = r; //store in array
r = (char)fgetc(inp);
}
arr[i][k]=0; //make last character of string null
if(feof(inp)){ //check again for EOF
break;
}
i++;
}
}
In C, it is common to use the function rand
to generate a random number between 0
and RAND_MAX
. The macro constant RAND_MAX
is guaranteed to be at least 32767
.
In order to get a random number between 0
and i
(not including i
itself), you can use the following expression, which uses the modulu operator:
rand() % i
This will not give you an even distribution of random numbers, but it is sufficient for most common purposes.
Therefore, in order to select and print a random word, you can use the following statement:
printf( "%s\n", rand() % i );
If you want to select and print 7 random words, then you can run this statement in a loop 7 times. However, it is possible that the same word will be selected randomly several times. If you want to prevent this from happening, then you will have to use a more complex algorithm, such as a Fisher-Yates shuffle.
However, this will print the same random sequence of words every time you run your program. If you want the random number generator to generate a different sequence of random numbers every time the program is run, then you must seed the random number generator, by calling the function srand
with some random data.
The simplest source of randomness is the current time. The function time
will return an integer representing the current time, usually in seconds.
srand( (unsigned)time(NULL) );
However, since the function time
usually uses seconds, this means that if you run the program twice in the same second, the random number generator will be seeded with the same value, so it will generate the same sequence of random numbers. If this is an issue, then you may want to find some other source of randomness.
After doing everything described above and adding the necessary headers, your program should look like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
FILE* inp;
inp = fopen("wordlist.txt","r"); //filename of your data file
char arr[100][6]; //max word length 5
srand( (unsigned)time(NULL) );
int i = 0;
while(1) {
char r = (char)fgetc(inp);
int k = 0;
while(r!='\n' && !feof(inp)) { //read till , or EOF
arr[i][k++] = r; //store in array
r = (char)fgetc(inp);
}
arr[i][k]=0; //make last character of string null
if(feof(inp)){ //check again for EOF
break;
}
i++;
}
//print 7 random words
for ( int j = 0; j < 7; j++ )
printf( "%s\n", arr[rand()%i] );
}
For the input
meal
cheek
lady
debt
lab
math
basis
beer
bird
thing
mall
exam
user
news
poet
scene
truth
tea
way
tooth
cell
oven
this program gave me the following (random) output:
user
mall
poet
lab
cheek
lab
beer
As you can see, one of the random words is a duplicate.
As previously stated, you can shuffle the array using a Fisher-Yates shuffle if you want to prevent the same word from being chosen twice. After shuffling the array, you can simply select and print the first 7 elements of the array, if that is the number of words that you want to chose:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main( void )
{
FILE* inp;
inp = fopen("wordlist.txt","r"); //filename of your data file
char arr[100][6]; //max word length 5
srand( (unsigned)time(NULL) );
int i = 0;
while(1) {
char r = (char)fgetc(inp);
int k = 0;
while(r!='\n' && !feof(inp)) { //read till , or EOF
arr[i][k++] = r; //store in array
r = (char)fgetc(inp);
}
arr[i][k]=0; //make last character of string null
if(feof(inp)){ //check again for EOF
break;
}
i++;
}
//perform a Fisher-Yates shuffle on the array
for ( int j = 0; j < i - 1; j++ )
{
char temp[6];
int k = rand() % ( i - j ) + j;
if ( j != k )
{
//swap both array elements
strcpy( temp, arr[j] );
strcpy( arr[j], arr[k] );
strcpy( arr[k], temp );
}
}
//print first 7 elements of the shuffled array
for ( int j = 0; j < 7; j++ )
{
//NOTE: This code assumes that i > 7, otherwise
//it may crash.
printf( "%s\n", arr[j] );
}
}
Now, the same word can no longer be chosen twice:
meal
thing
news
user
mall
exam
tea
In my program above, I shuffled the entire array. However, if I only need the first 7 words to be randomized, then it would be sufficient to only perform 7 iterations of the outer loop when shuffling.