0

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?

Character() 
	{
		cout << "Enter name >>  ";
		cin >> this->name;
		cout << endl;
		//RANDOM NUMBER GENERATOR FOR STAT ROLL
		srand((unsigned int)time(0));
		int random = ((rand() % 20) + 8);
		//ASSIGN CHARACTER STATS
		level = 1;
		strength = random;
		dexterity = random;
	}
5
Contributors
10
Replies
12
Views
7 Years
Discussion Span
Last Post by firstPerson
Featured Replies
0

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.

0

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?

0

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

0

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.

0

Your mistake is this

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

Random is copied twice.

Try this

random=(rand() % 20) +8;
strength=random;
random=(rand() % 20) +8;      // GET A NEW VALUE FOR RANDOM
dexterity=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)

Edited by StuXYZ: n/a

1

>>First_person

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

Votes + Comments
lol, more code reviews for me.
0

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'

0

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>

0

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;

Edited by firstPerson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.