Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Using Rand()

Credits go to Krzysztof Kluczek for helping out!
This thread is locked; no one can reply to it. rss feed Print
Using Rand()
Dragonsoulj
Member #7,678
August 2006

Well, first things first. This program is meant to create a random number between 1 and however high the max is. It is basically a dice rolling simulation. I have fixed some problems that the compiler returned, but I need help.

1//gamemech.cpp
2#include <allegro.h>
3#include <cstdlib>
4 
5class Die_roll{
6 //Left Blank For Possible Privates
7 public:
8 int dieroll;
9 int srand(unsigned dieroll);
10 void roll_die(int){
11 rand();
12 }
13 friend class D4;
14 friend class D6;
15 friend class D8;
16 friend class D10;
17 friend class D12;
18 friend class D20;
19}
20 
21class D4{
22 #undef RAND_MAX
23 #define RAND_MAX 4
24 public:
25 int roll_die;
26}
27 
28int main(){
29
30 allegro_init();
31 install_keyboard();
32 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
33 set_display_switch_mode(SWITCH_PAUSE);
34 set_window_title("Trial");
35
36 D4 xdieroll;
37 xdieroll.roll_die;
38 //Conversion from xdieroll to printable text here
39 textout_ex(screen, font, /* Printable Text */, 10, 10, makecol(0, 0, 255), -1);
40
41 readkey();
42
43 return 0;
44}
45END_OF_MAIN()

This gives me a few errors.

    In function `Die_roll _mangled_main()':

      I need to fix this so that I can get an integer (doesn't matter how just so that it is positive) from the D4 through D20 list. If I can get the D4 fixed, then I can just change the RAND_MAX to whatever number I need. This is where the last two comments come in. I need a conversion apparently to get to use the result, and I wanted to have it printed to test the program to make sure it works. Please help. I am confused ??? right now.</li>

      CursedTyrant
      Member #7,080
      April 2006
      avatar

      Wouldn't it be just easier to use something like this?

      1#define DIES 6
      2 
      3vector<int> dierolls;
      4int dies[DIES] = { 4, 6, 8, 10, 12, 20 };
      5 
      6void rolldie(int die)
      7{
      8 dierolls.push_back(rand()%die+1);
      9}
      10 
      11int main()
      12{
      13 for (int i=0; i<DIES; i++) { rolldie(dies<i>); }
      14
      15 for (int i=0; i<dierolls.size(); i++)
      16 { textprintf_ex(screen, font, 10, 10, makecol(255, 255, 255), -1, "D%d: %d", dies<i>, dierolls<i>); }
      17
      18 return 0;
      19}

      ---------
      Signature.
      ----
      [My Website] | [My YouTube Channel]

      Elverion
      Member #6,239
      September 2005
      avatar

      Wait a second...why are you bothering with all that D4..D20 stuff? It would be easier (and better) to keep RAND_MAX at some arbitrary value (ex: 65536), and then just use the mod operator( % ) to return the remainder of the division.

        unsigned int random_number(unsigned int sides)
        {
          return ( Rand() % die_sides );
        }
      

      I don't quite understand why you have "xdieroll.roll_die" in your code. EDIT: You forgot the () there, since roll_die is also what you named your function. Please do not name variables and functions the same thing if at all possible :-/. Also, make it public function that returns an unsigned integer instead.

      You are undefining and redefining RAND_MAX in D4. Even if you were to get it to work that way, defines are at compile-time, not run-time, so no mater which friend class you used, it would give the same result.

      --
      SolarStrike Software - MicroMacro home - Automation software.

      Krzysztof Kluczek
      Member #4,191
      January 2004
      avatar

      Elverion: You forgot +1, misspelled parameter name and used upper case in "rand" function name. The code should look as follows: :)

      unsigned int random_number(unsigned int die_sides)
      {
        return ( rand() % die_sides ) + 1;
      }
      

      Audric
      Member #907
      January 2001

      The symbol RAND_MAX is already defined in libc, it is usually not a good idea to 'recycle' it.

      I kinda remember classes require a final ; like
      class Die_roll{
      // stuff
      };
      This should explain the 'line 28' messages.

      Why do you declare "int srand(unsigned dieroll);" in your class ? it is a libc function you should call. Just once.

      Even when it will compile, there is a lot of stuff in there that doesn't make much sense... good luck

      Dragonsoulj
      Member #7,678
      August 2006

      Elverion said:

      I don't quite understand why you have "xdieroll.roll_die" in your code.

      I don't really remember why I started to use that part of the code. I had to change the variable name so it doesn't match the rest, and I did forget to add the (). I will try not to name the functions the same in the future.

      Krzysztof Kluczek said:

      unsigned int random_number(unsigned int die_sides)
      {
        return ( rand() % die_sides ) + 1;
      }
      

      Thanks. Just for future reference, could:

      unsigned int random_number(unsigned int die_sides)
      {
          srand(1);
          return ( rand() % die_sides );
      }
      

      work as well?

      Jonatan Hedborg
      Member #4,886
      July 2004
      avatar

      yes, but that would return the same value every time.
      srand sets the seed. It should be done once. Either with a value (then you will get the same series of values each time) or with time(0) (for "real random" behaviour).
      also, you still need the +1

      -------
      Sweden: Free from the shackles of Democracy since 2008-06-18!

      Elverion
      Member #6,239
      September 2005
      avatar

      I would throw that call to srand into a new function, void Init(). You could, if you choose, put it in the default constructor, but then remember it would be called every time you create an instance of that object.

      Krzysztof Kluczek: Well, don't I feel like an ass.

      --
      SolarStrike Software - MicroMacro home - Automation software.

      Krzysztof Kluczek
      Member #4,191
      January 2004
      avatar

      Elverion: I just wanted to be helpful and make this code more useable example. :)

      Tobias Dammers
      Member #2,604
      August 2002
      avatar

      OMG, way over the top.

      unsigned roll_dice(unsigned sides) {
        return (rand() % sides) + 1;
      }
      
      unsigned roll_multi_dice(unsigned nr_dices, unsigned sides) {
        unsigned result = 0;
        for (unsigned i = 0; i < nr_dices; ++i) {
          result += roll_dice(sides);
        }
        return result;
      }
      

      There. You don't need no complicated class hierarchy just to roll a dice.

      ---
      Me make music: Triofobie
      ---
      "We need Tobias and his awesome trombone, too." - Johan Halmén

      Jonatan Hedborg
      Member #4,886
      July 2004
      avatar

      But what about all the pretty over-design? D:

      -------
      Sweden: Free from the shackles of Democracy since 2008-06-18!

      Tobias Dammers
      Member #2,604
      August 2002
      avatar

      It doesn't use templates nor singletons, so it's not half bad.
      I'd suggest you add a few useful defines:

      #define ONE 1
      #define TWO 2
      #define THREE 3
      #define FOUR 4
      #define FIVE 5
      // snip
      #define TWO_MILLION_SIX_HUNDRED_AND_FORTY_FIVE_THOUSAND_TWO_HUNDRED_AND_THIRTEEN 2645213
      

      That should cover all numbers you'll ever need. You could even write a program that automatically writes these defines into a dedicated header! Then to roll the dice using my function, you can do this:

      unsigned r = roll_dice(FOUR); // Rolls a 4-sided dice, storing the result in r
      if (r == ONE) // if r equals 1...
        do_stuff_one(); // do "one"-stuff
      if (r == TWO) // if r equals 2...
        do_stuff_two(); // do the "2"-stuff
      

      Hehe. Anyone's stomach turning yet?

      ---
      Me make music: Triofobie
      ---
      "We need Tobias and his awesome trombone, too." - Johan Halmén

      Go to: