Thread: generating random words

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    generating random words

    Hi I am trying to teach myself C, and as an exercise I decided to try and make a program that generates random words which contains however many letters the user chooses. So far I have the user able to choose how ever many letters they want, and it will print out that many "characters. Here are my problems. First off, the random function isnt random; if you choose 5 characters it will always spit out the same thing. Next I am trying to have the comp choose the characters from an array of the 26 letters. The problem is is that the characters you see on the screen are not letters! Here is my script so far:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      char letters [26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
      char randn(char letters) //random definition
     {
       return rand()%letters;
     }
      int choose;
      int length;
      int logic = 0;
      printf("How many letters should your word consist of?\n");
      scanf("%i",&length);
      if(length>0)
      {
        while(logic == 0, logic <=length) //loop that displays the text
        {
          ++logic;
          choose = randn(letters);
          printf("%c", choose);
          
        }
      }
      else
      { 
        printf("A word must contain at least one letter!\n\n");
      }
      printf("\n"); //seperate
      system("PAUSE");	
      return 0;
    }

    I want to learn as much of this on my own as possible, so please try to give to much away, and also a thorough explanation of what I am doing wrong would be great.

    thankyou in advance.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    http://members.cox.net/srice1/random/crandom.html

    Also it is [code][/code] But atleast you tried, kudos for that.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    I looked at it and it didnt seem to help much, but thanks for the help. That was focused on numbers, and I want it to randomlly choose a letters from an array of characters. This might explain part of my problem better which is that it isnt actually outputing letters but odd charaters:

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    That article was probably a little over your head, since it argues against using
    rand () % num; due to that approach wrecking the normal distribution (statistical
    terms). Read this

    Also, think about the following: where in you code do you access the array?

    Look at this, and see if you can find a way to scrap that array altogether

    One more thing: a function definition should not be within another function:

    Code:
      char randn(char letters) //random definition
     {
       return rand()%letters;
     }
    should not be inside main.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Hmmm, i might need a little more hand holding, . I dont see how setting a range or anyhting will help. Would Iuse stx and etx to declare when the array starts and stops? How do I have the rand() function access the array? Sorry if I am a little slow with this.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You have an array of all the letters... your range should start at the first index of that array (0) then the last index of the array (25). Then you take the number you randomly generated and use that as the number to access the part of the array.

    Another thing to consider... all data in a computer is stored as numbers.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Would you use pointers to make the char obtain the values of an int (So *letters[a] = nums[1]; ). wow, I had better take a step back.
    Last edited by lilrayray; 07-21-2006 at 01:03 PM.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    169
    pointers are not necessary to do this.
    letters[0] would give you the character 'a'
    letters[1] == 'b'
    and so forth.

    Now you could randomly generate a number and use it, as Wraithan said, as the index for the array, so letters[RandomInteger] will give you a random letter. Though make sure you generate a number from 0 to 25.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Oh I get it now. What I was getting confused with was I kept thinking array[a] instead of array[0]. So I have fixed it up and it does generate letters but it is not random. Any hints on making the choice random would be very kind. here is what I hav got by the way:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      char letters [26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
      int choose;
      int length;
      int logic = 0;
      printf("How many letters should your word consist of?\n");
      scanf("%i",&length);
      if(length>0)
      {
        while(logic == 0, logic <=length) //loop that displays the text
        {
          ++logic;
          choose = rand()%25;
          printf("%c", letters[choose]);
        }
      }
      else
      { 
        printf("A word must contain at least one letter!\n\n");
      }
      printf("\n"); //seperate
      system("PAUSE");	
      return 0;
    }

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    you need to seed the rand... look at those links and read them.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    169
    What is going on with your while loop?
    try something like:
    for (logic = 0; logic < length; logic++)

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Alright it works just like I wanted it to. Thankyou for the help. I think Ill read up more on srand() to get a better understanding of it. Now my next step is to set rules for the word structures, but I think I shall do this with out any help.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    20
    good job, you figured it out with minimal help.

    This project looks alot like one of my first projects. The idea was that the program would generate 8 random characters, and the user would then use those 8 character as a password. So it basically did the same thing you did. Only, instead of using an array, I used a switch/case statment, and ended up coding alot more lines to accomplish the same thing you did.

    Code:
    switch(randletter)
    {
      case 0:
        letter[x] = 'a';
        break;
      case 1:
        letter[x] = 'b';
        break;
    ....
    ....
    ....
    So your array was some smart thinking, pat yourself on the back

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I learned to do it the first time with itoa() but I didn't want to add in more stuff while he was still having problems with rand. Look up that function, it is pretty interesting.

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    It looks interesting. What are some practical uses for it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Replies: 1
    Last Post: 12-14-2002, 01:51 AM