Can anyone offer an example on this?

This is a discussion on Can anyone offer an example on this? within the C++ Programming forums, part of the General Programming Boards category; How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Can anyone offer an example on this?

    How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but what I tried doesn't work... at all.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Can anyone offer an example on this?

    Originally posted by drdroid
    How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but what I tried doesn't work... at all.
    Maybe you should show us what you've tried to do so far. Random number generation is covered in quite a few posts on these boards.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to create an emu of the monster names and randomly generate an integer representing the monster ID.

    Code:
    emu monster {A = 0x0001, B, C, D, E};
    unsigned int nNum = 1 + rand() % 5;
    
    switch (nNum)
    {
       case A : { break; }
    ...
    }
    Kuphryn

  4. #4
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    I dont mean to be rude, but theres an example of correct, yet extremely overly complex code.

    What was wrong with:

    Code:
    switch ( (rand() % 5) + 1 )
    {
       case 1 : break; //theres no need for enclosing braces in a case
    ...
    }
    ?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and one of the more common links I post is...
    http://www.eskimo.com/~scs/C-faq/q13.16.html

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I'm not talking about random number generation... I know how to do that... I'm talking about applying that to choosing a monster for my battle engine. I'm more concerned about the string part... not the number.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use a random number to index a static array of strings.Arrays start at array[0] and finish at array[x] so you need rand(x). Simple.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by lightatdawn
    /theres no need for enclosing braces in a case
    They are needed on some compilers in some situations. It doesn't hurt to put them there, it prevents scope errors.

  9. #9
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>They are needed on some compilers in some situations

    AFAIK, it should be all compilers, but the only situation (again, AFAIK) is when attempting to declare variables inside the scope of the case. This seems like a bad coding practice anyhow, so I dont see why one would want to condone it. Though your right, it doesnt _technically_ hurt.

    >>use a random number to index a static array of strings.Arrays start at array[0] and finish at array[x] so you need rand(x). Simple.

    Yup. Listen to the man.

    Monster->SetupStats(rand() % Max_Monsters);
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why doesnt the C++ standard library committee offer any AUI related libararie?
    By Masterx in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-13-2009, 12:48 PM
  2. does c++ offer diffs of datastrucures?
    By pheres in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2007, 07:41 AM
  3. Please offer some critisism
    By NickESP in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 03:16 PM
  4. SMS provider that offer SMS shipping world-wide for under 0,05 ???
    By gicio in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-26-2003, 08:26 PM
  5. My job offer!!
    By Inept Pig in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-09-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21