Okay. I created a program to create a character, and it uses a random number generator to initialize the Character classes strength and dexterity.

Here's the character class constructor.
Since the random number generator is based on time, both strength and dexterity have the same value when they're initialized. Is there a way to make them both different random numbers?

		cout << "Enter name >>  ";
		cin >> this->name;
		cout << endl;
		srand((unsigned int)time(0));
		int random = ((rand() % 20) + 8);
		level = 1;
		strength = random;
		dexterity = random;

When seeding a pseudo random number generator with a certain number (that's what srand does), it will always produce the same series of "random" numbers.
So you should call srand just once at the beginning of the program.

I called srand just once in the whole program and it was in that constructor. So is there no way for it to make a random number for dexterity after it's assigned strength since time isn't changing when it's being initialized?

Um... well, you're assigning the same number to strength and dexterity.
So how about generating a second random number?

First take srand( time(0) ) out of the constructor. Put srand( time(0) ) at the beginning at main.

Second do this :

strength =  (rand() % 20) + 8;
dexterity = (rand() % 20) + 8;

So that way they both get different values range from 8 to 27.

Your mistake is this

int random=(rand() % 20)+8;
dexterity=random;   // copy random again.

Random is copied twice.

Try this

random=(rand() % 20) +8;
random=(rand() % 20) +8;      // GET A NEW VALUE FOR RANDOM

Obviously you can remove the intermediate variable (random) e.g

strength=(rand() % 20)+8;
dexterity=(rand() % 20)+8;

EDIT: First_person just submitted it first -- he is correct (as well)


Excuse me, I use mixedCaseForMyName, as in firstPerson :)

commented: lol, more code reviews for me. +3

I did everything you said firstPerson, except move the srand() out of the constructor and it works fine. Whenever I put the srand() in global scope I get these errors.

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2365: 'srand' : redefinition; previous definition was 'function'

Also, I didn't mean for it to be 8-27. I thought this would make it 8-20. What numbers do I have to change around for that>

Cut off the upper end of the range to account for the addition.

rand() % (20-8) + 8

Cut off the upper end of the range to account for the addition.

rand() % (20-8) + 8

That code does not go up to 20. It goes up to 8-19. This code produces in range of 8-20.

int random = rand() % 13  + 8;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.