Thread: Random Outputs.

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    8

    Random Outputs.

    Hey what if you need a program to randomly choose several funtions from a list. Sort of like a hello program that responds in different ways.

    I was wondering how to do this.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    73
    You could make a random number and use if statements or a switch statement to call the functions. Give us an example and maybe we can help.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Well, I'm making a chat bot thing (in cmd promt) using Dev C++.

    This is what I have:

    Code:
    cout << "what is your name?" << endl;
    cin >> inputName
    
    if(inputName>= 20 char) cout << "Your REAL name, please." << endl;
    
    else
    
    cout << Thats a very funny name, << inputName << ". Very funny..." << endl;
    NOTE: Thats not all I have. I got the headers, declarations and other stuff too.

    So now I need to have the bot say different things. So I'll have 4 other possible sayings, and the bot will randomly choose to say them with the input.

    Need help on that.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    For what you are describing I think you'd probably be better off setting up an array of strings, then randomly select a response from the array. This way you could have an array of questions, array of statements, array of jokes etc.

    If however you would like to randomly select functions, you can setup an array of pointers to functions then randomly select them. Here's an example....

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    void function1();
    void function2();
    void function3();
    
    int main()
    {
        int randFunc;
        void ( *func[3] )() = { function1, function2, function3 };
        
        srand ( time(NULL) );
        randFunc = rand() % 3;
        
        ( *func[randFunc] )();
        
        cin.get();
        
        return 0;
    }
    
    void function1()
    {
        cout << "Function 1 executed." << endl;
    }
    
    void function2()
    {
        cout << "Function 2 executed." << endl;
    }
    
    void function3()
    {
        cout << "Function 3 executed." << endl;
    }

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    as this looks like a very simple chat bot, you can score reponses (the scoring is upto your choosing), depending on the value of the score you display the proper response. This, however, is a highly inefficient way to do a chatbot. I suugest looking at http://www.alicebot.org/ for ideas and inspiration.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    For what you are describing I think you'd probably be better off setting up an array of strings, then randomly select a response from the array. This way you could have an array of questions, array of statements, array of jokes etc.

    If however you would like to randomly select functions, you can setup an array of pointers to functions then randomly select them. Here's an example....
    yeah, that is an idea, but if he wants to simulate some sort of intelligent response, random responses wont work.

    But to follow Scribbler's thought, you could have an array of, lets say, "comforting" responses. You can check your user's input, and after parsing it you see that it contains, for example, the string "sad", then you randomly choose a response from your "comforting" array - you can do the same for different emotions - again, it might be good practice, but look into something more efficient and expandable.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] ISBN Check Digit (random outputs)
    By Dakaa in forum C Programming
    Replies: 3
    Last Post: 04-21-2009, 08:53 PM
  2. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  3. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM