Thread: Is it possible to generate random text?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps something like this:
    Code:
    const char *win[] = {
        "You win!", "Congratulations, you just won!", "You seem to have beaten me . . .", "You won . . . this time"
    };
    
    cout << win[rand() % (sizeof(win)/sizeof(*win) )] << endl;
    * Sorry, Prelude.

    [edit] Too slow. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You mean Salem?

    Consider the language(text) you are using and create lists of verbs, adverbs etc from which you can make random selections following simple grammatical rules to create what appears, superficially anyway, to be distinct sentences. This is really a variation on what's been previously described, but here's an example to give you some ideas
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    namespace
    {
    std::vector<std::string> adjectives;
    std::vector<std::string> adverbs;
    std::vector<std::string> verbs;
    }
    
    void SetAdjectives();
    void SetAdverbs();
    void SetVerbs();
    
    int main()
    {
    SetAdjectives();
    SetAdverbs();
    SetVerbs();
    
    std::string monster="rat";
    
    srand(time(0));
    
    std::cout<<"You "<< 
             adverbs.at(rand()%adverbs.size())<<
             " "<<
             verbs.at(rand()%verbs.size())<<
             " the "<<
             adjectives.at(rand()%adjectives.size())<<
             " "<<monster<<std::endl;
    }
    
    void SetAdjectives()
    {
    //..or load from file
    adjectives.push_back("cursed");
    adjectives.push_back("horrible");
    adjectives.push_back("wicked");
    adjectives.push_back("rancid");
    adjectives.push_back("churlish");
    adjectives.push_back("dirty");
    }
    
    void SetAdverbs()
    {
    //..or load from file
    adverbs.push_back("deftly");
    adverbs.push_back("swiftly");
    adverbs.push_back("efficiently");
    adverbs.push_back("maniacally");
    adverbs.push_back("smugly");
    adverbs.push_back("whimsically");
    }
    
    void SetVerbs()
    {
    //..or load from file
    verbs.push_back("biffed");
    verbs.push_back("chopped");
    verbs.push_back("flensed");
    verbs.push_back("hit");
    verbs.push_back("struck");
    verbs.push_back("whacked");
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  2. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM