Thread: random functions, is it possible?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    random functions, is it possible?

    I was going to start text based game for practice, and while i was just thinking about how i could make it all work, i was wondering is there anyway to call a random funciton? like if i make a random number 1-5 if its 1 rat() is called ect?... i seems every time i poste a question i come up with a good answer half way through, but any help is apreciated

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Does this not work?:
    Code:
    int num = 1 + (5.0 * rand()) / RAND_MAX;
    if (num == 1) rat();
    else if (num == 2) fat();
    else if (num == 3) cat();
    else if (num == 4) tru();
    else dat();
    Or, alternately, use a table of function pointers and lookup the function. Or use a switch statement.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    thats the code i was talking about being a possible answer, i haven't started on the program yet, i kinda just work up and decided to make a game... ill try and i gotta get my gf right now so ill be back at the comp in a while
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    what headers do i include to do random numbers? (i've never used random numbers before, and the tutorial on this site isn't very helpful)
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    rand() and srand() are declared in <cstdlib>. A lot of people like to seed the RNG with the system time, and you can find time() in <ctime>. Here's the most common way to make random numbers:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() {
      srand((unsigned)time(0));
    
      for (int i = 0; i < 20; i++)
        cout << rand() % 10 << ' ';
      cout << '\n';
    
      return 0;
    }
    You should know that this way sucks ass. Using time() as the argument to srand() has subtle issues, rand() isn't a good RNG to begin with, and using % to force the numbers into a smaller range makes it even worse. That's your fair warning, but for simple stuff it really doesn't matter because it'll look random enough.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    i compiled your source, and it worked fine, but where do you declare rand()? and where do you declare int i ?(im not real good with technical terms, but i think declare is the word im looking for)

    edit: duh (i should read more lol rand() is declared in cstdlib, but what about int i?
    Last edited by sreetvert83; 09-02-2005 at 10:43 AM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>rand() is declared in cstdlib, but what about int i?

    the int data type is "built in", so to speak, along with double, char, float, etc. You might be able to find details about the type in one of the major header files like iostream.

    The variable i is declared in the following line:

    for (int i = 0; i < 20; i++)

    If you have an up to date compiler this should restrict the scope of the variable i which is of type int to the for loop within which it is declared.
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    so would it work just the same declaring the variable int i somewhere else?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    My personal preference for such a task would be to use a std::map from the key type (integers) to a "task" object pointer, where the task object is an abstract base for your functionality. This allows for a great deal of flexibility, without too much work.

    Task could define an appropriate interface, and then subclasses would implement specific functionality. A good hierarhcy would be something like:

    task
    - atomic_task
    -- rat( )
    -- cat( )
    -- etc...
    - composite_task
    -- rat_and_cat( ) ... etc
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Random function's application:Dice Roller
    By angeljicu in forum C Programming
    Replies: 0
    Last Post: 11-02-2002, 05:29 AM