|
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.
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
|
Wouldn't it be just easier to use something like this?
--------- |
Elverion
Member #6,239
September 2005
|
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. -- |
Krzysztof Kluczek
Member #4,191
January 2004
|
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 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: Thanks. Just for future reference, could: work as well? |
Jonatan Hedborg
Member #4,886
July 2004
|
yes, but that would return the same value every time.
|
Elverion
Member #6,239
September 2005
|
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. -- |
Krzysztof Kluczek
Member #4,191
January 2004
|
Elverion: I just wanted to be helpful and make this code more useable example. ________ |
Tobias Dammers
Member #2,604
August 2002
|
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. --- |
Jonatan Hedborg
Member #4,886
July 2004
|
But what about all the pretty over-design? D:
|
Tobias Dammers
Member #2,604
August 2002
|
It doesn't use templates nor singletons, so it's not half bad. #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? --- |
|