简体   繁体   中英

How to use Rand function to generate a random word using a length ranging from 3 to 9 characters in C?

I am attempting to use two for loops to generate a random sequence of words that range between 3 and 9 characters and that will run a total of 1000 times.

The inner for loop generates a random single letter which then will combine to generate words that range from 3-9 characters.

So far, I have:

#include <stdio.h>
#include <time.h>

int main()
{   
    int i, j; 
    int RandomNumber; 
    char letter[0]; 
    char word[8];
    char testword[4] = "test";

    // generate random word 

    for (j = 3; j < 10; j++) {
        RandomNumber = rand() % 26;
        printf("%d", RandomNumber); 
        word[j] = 'a' + RandomNumber; 
        printf("Word: %s", word[j]); 
    }

    return 0;
}

Continuing from my comment, you are currently overrunning the bounds of word using your loop indexing of for (j = 3; j < 10; j++) . You additionally have no idea what will be in word[0-2] as word is not initialized and you never fill indexes 0-2 with anything. Also, in C when you declare char word[8]; there are only 8 characters that you can add to word in indexes 0-7 .

If you intend to use the array as a character-string , then C requires that the string be nul-terminated (eg have the nul-byte , ie 0 or '\\0' (they are equivalent) as the last character of the string). Attempting to use word as a string without being nul-terminated is undefined behavior (eg calling printf("Word: %s", word[j]); on word without it being nul-terminated )

You don't need two loops, you just need a random length between 3-9 for each of the words you will generate. This can be a separate call to rand() before entering the loop to generate the characters for word .

To fix all of the problems (and noting the inclusion of stdlib.h for rand() and srand() ), you could do something similar to the following:

#include <stdio.h>
#include <stdlib.h> /* for rand()/srand() */
#include <time.h>

enum { MINW = 3, MAXW = 9 };    /* constants for min/max length */

int main (void)
{
    int i, randlength;
    char word[MAXW+1] = "";     /* initialize your variables */
    i = randlength = 0;

    srand (time (NULL));        /* initialize the random number generator */
    randlength = rand() % (MAXW - MINW + 1) + MINW;  /* randlength of 3-9 */
    printf ("length : %d\n\n", randlength);

    for (i = 0; i < randlength; i++) {
        int randomnumber = rand() % 26,
            randchar = 'a' + randomnumber;
        printf(" number[%2d] : %2d  '%c'\n", i, randomnumber, randchar);
        word[i] = randchar;
    }
    word[i] = 0;    /* nul-terminate (note: also done by initialization) */
    printf ("\nWord : %s\n", word);

    return 0;
}

Example Use/Output

$ ./bin/randword       
length : 3

 number[ 0] :  8  'i'
 number[ 1] : 20  'u'
 number[ 2] :  0  'a'

Word : iua

$ ./bin/randword
length : 8

 number[ 0] : 24  'y'
 number[ 1] : 13  'n'
 number[ 2] : 12  'm'
 number[ 3] : 14  'o'
 number[ 4] :  9  'j'
 number[ 5] :  6  'g'
 number[ 6] :  9  'j'
 number[ 7] : 12  'm'

Word : ynmojgjm

It is important you understand the comment following word[i] = 0; "also done by initialization" . Think about why that explicit nul-termination could be eliminated there. Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM