Thread: Picking out random words

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69

    Question Picking out random words

    Hi. I am new to C++ and I want to pick out random words and store them in a variable. Any ideas?
    Thanks,
    Adam

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    select a file, such as the Declaration of Independence, or the Constitution, or War & Peace, generate a random number and read that word number from the file. Read the file one word at a time until the desired word number is read.

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Random words from what? A file? An array? The most effective way to do it depends on where the words are stored when you take a random sample.
    Just because I don't care doesn't mean I don't understand.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could do this using C-strings or STL strings (the string class). But since I don't know what method of strings you've learned, I'll tell you how to do this using C-strings. It very well may be easier with STL strings, and someone else can help you out with that more than I can.

    First, you need to read in the FAQ about how to generate random numbers. Then, you can use a function in <cstring> called strtok() that separates a string into different sections based on a delimiting character you specify. Since you're counting words, you would use a space as your delimiter. You would use strtok() in a loop to 'tokenize' the whole string. strtok() returns a pointer to the section it just tokenized, so you would assign these pointers to an array of char *'s. Use your new random-number generating abilities to pick out certain words from this array, and then store them any way you wish.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    It would be prefferable to select the word from an array.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> It would be prefferable to select the word from an array.

    And these words get into this array how? If they are in an array already (char *[], std::string []) then you can simply index your someArray[rand() % ARRAY_LIM] ARRAY_LIM being the size of your array.

    Edit: >> select a file, such as the Declaration of Independence, or the Constitution, or War & Peace, generate a random number and read that word number from the file. Read the file one word at a time until the desired word number is read.

    Is that your assignment that you posted under an alterate account ?

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Can I just pull it out of an enumerated list?

  8. #8
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Why not:
    Code:
        char String[100];
    
        srand( ( unsigned ) time ( NULL ) );
        Random = rand() % 10;
        
        switch ( Random ){
               
        case 0 : strcat ( String, "word0" );
        break;
        case 1 : strcat ( String, "word1" );
        break;
        case 2 : strcat ( String, "word2" );
        break;
        case 3 : strcat ( String, "word3" );
        break;
        case 4 : strcat ( String, "word4" );
        break;
        case 5 : strcat ( String, "word5" );
        break;
        case 6 : strcat ( String, "word6" );
        break;
        case 7 : strcat ( String, "word7" );
        break;
        case 8 : strcat ( String, "word8" );
        break;
        case 9 : strcat ( String, "word9" );
        break;
        default : strcat ( String, "word10" );
        break;
    Maybe is that what you mean, I don't now but I hope this helps you.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Hey Ancient Dragon,
    how can I choose a random word from a file,
    by the way, thanks Yuri, i'll try that.
    -Adam

  10. #10
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Hey Yuri,
    My compiler found a bug in this line:
    Code:
    Random = rand() % 10;
    and this line:
    Code:
    case 0 : strcat ( String, "word0" );
    As I said before, I am new in C++ and am quite helpless in the face of a bug.
    Do you know what is wrong with it?
    P.S. How do I print out my results? eg. Is Random the variable?
    Thanks alot,
    Adam
    Adam

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    beanroaster: I recommend you continue with the book or tutorials or whatever you are learning C++ from before continuing with this project. You're basically asking us to provide you source code based on very few details, and you have trouble understanding that source code.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    My book doesn't have randomization in it. Can you reccomend any tutorials with that in it?
    Otherwise if you need more details, I am trying to generate random sentances using all random words that are pulled from seperate arrays. In the end I would want to be able to simply call down the variables like this:
    Code:
    cout << word1 << " " << word2 << " " << word3 " "
    etc...
    I see your point though.
    Thank you for your help,
    Adam

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Quote Originally Posted by beanroaster
    Can you reccomend any tutorials with that in it?
    Quote Originally Posted by sean_mackrory
    read in the FAQ about how to generate random numbers
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by beanroaster
    Hey Ancient Dragon,
    how can I choose a random word from a file,
    by the way, thanks Yuri, i'll try that.
    -Adam
    Do you know how to open a file and read it? If not you should study that first. Basically this is how. NOTE: I did not compile or test this program.

    Code:
    #include <ctime>
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string word;
        // generate random seed
        srand( time(0) );
       // select a random number
       int num = rand();
      // open the file
      ifstream in("filename.txt");
      if( in.is_open() )
      {
          int i = 0;
          while( !in.eof()  && i != num)
          {
             in >> word;
            ++i;
         }
         // did we fine the right word?
        if( i != num )
        {
             // not that many words in the file.  so just bail out
             cout << "only " << i << " words in the file." << endl;
        }
        else
        {
           cout << "Word number " << i << " is " << word << endl;
        }
        return 0;
    }
    Last edited by Ancient Dragon; 09-03-2005 at 07:10 PM.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    thanks
    Adam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  2. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  3. Replies: 1
    Last Post: 12-14-2002, 01:51 AM
  4. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM