Public Group

# Having problem with Random Generator

This topic is 4452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having trouble generating random numbers. This is my first time trying it. I am just making a small program for practice since Im a complete newbie. I want to be able to select the number values for 3 attributes but in the class constructor I want to have a random generation of the attributes. To explain better I am trying to do one of the exercises from the work sessions. I can't remember which week though. I tested it quite early by printing out two attributes from two Classes and they both are getting the same number randomly generated. I am only calling srand once as well. take a look. Here is my main.cpp file:

#include "character.h"

Character::Character()
{
int randomNum = ( rand() % 20 ) + 1;
}
Character::~Character()
{
}

Character::Character(int agi, int dex, int str)
{
int itsAgi = agi =( rand() % 20 ) + 1;
int itsDex = dex =( rand() % 20 ) + 1;
int itsStr = str =( rand() % 20 ) + 1;

}

int main()
{

srand( (unsigned)time( NULL ) );
Character Elf;
Character Dwarf;
cout &lt;&lt; Elf.GetAgi() &lt;&lt; endl &lt;&lt; Dwarf.GetDex() &lt;&lt; endl;
}


And here is the header file character.h:
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

using namespace std;

class Character
{
public:
Character(int agi, int dex, int str);
~Character();
Character();
int GetAgi() const {return itsAgi;}
int GetDex() const {return itsDex;}
int GetStr() const {return itsStr;}
void SetAgi(int agi){itsAgi = agi;}
void SetDex(int dex){itsDex = dex;}
void SetStr(int str){itsStr = str;}
private:
int itsAgi;
int itsDex;
int itsStr;
};


I keep getting the number -858993460. what am I doing wrong? thanks [Edited by - petelta on October 5, 2006 1:05:43 AM]

##### Share on other sites
You are using your default constructor which does not assign any of the members.

Please also note that your 3-parameter constructor will pretty much ignore any parameter you pass it, since you overwrite them with random numbers.

As for formatting, see the FAQ.

##### Share on other sites
ok so should I enter the code in my other constructor into the default constructor area so that it will give 3 paramters?

also, by having the random generator change the paramaters I pass in, I should take out the random generation code there?

##### Share on other sites
ok figured it out, thanks for the help. Here is my final code for those who want to know. If you see anything terribly wrong with it, let me know;

#include "character.h"Character::Character(){	int randomNum = ( rand() % 20 ) + 1;	itsAgi = (rand() % 20 ) + 1;        itsDex = (rand() % 20 ) + 1;        itsStr = (rand() % 20 ) + 1;}Character::~Character(){}Character::Character(int agi, int dex, int str){	itsAgi = agi;	itsDex = dex;	itsStr = str;}int main(){		srand( (unsigned)time( NULL ) );	int strength;	int dexterity;	int agility;	Character Travis;	Character Daniel(5,5,5);	cout << Travis.GetAgi() << endl << Daniel.GetDex() << endl;}

##### Share on other sites
Here is what I propose:

Default constructor: set all the attributes to a random value.
3-parameter constructor: set the attributes to the specified value.

Character::Character() :   itsAgi( rand() % 20 + 1 ),   itsDex( rand() % 20 + 1 ),   itsStr( rand() % 20 + 1 ){}Character::Character(int agi, int dex, int str) :   itsAgi(agi), itsDex(dex), itsStr(str){}

If you aren't familiar with the above syntax, it is an initialisation list. It basically is the list of parameters that get passed to the member variables's constructors. Using it is generally preferable to doing an assignment in the constructor body.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633689
• Total Posts
3013337
×