Sign in to follow this  
petelta

Having problem with Random Generator

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 << Elf.GetAgi() << endl << Dwarf.GetDex() << endl;
}

And here is the header file character.h:
#include <iostream>
#include <stdlib.h>
#include <time.h>

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this