Public Group

# 3d6 Stat Generator

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

## Recommended Posts

Okay, so I wanted to make a simple stat generator for generating perfect stats during character creation in a pen-and-paper RPG, since I seem to suck at rolling dice. It should be simple. Just make some stat variables, give 'em a number, and if it, and all the other stats, is 18, break. Now, I have two simple problems. 1. If one stat is 18, the while loop breaks. 2. The numbers change every second, instead of each iteration.
#include <iostream.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
int STR=0;
int IQ=0;
int LUK=0;
int DEX=0;
int CON=0;
int CHR=0;

srand(time(NULL));

while(STR < 18 && IQ < 18 && LUK < 18 && DEX < 18 && CON < 18 && CHR < 18)
{

STR = (rand() % 16) + 3;
IQ = (rand() % 16) + 3;
LUK = (rand() % 16) + 3;
DEX = (rand() % 16) + 3;
CON = (rand() % 16) + 3;
CHR = (rand() % 16) + 3;
cout << "STR:"<< STR << " IQ:" << IQ << " LUK:" << LUK << " DEX" << DEX <<" CON:" << CON << " CHR:" << CHR << "\n\n";
}

system("PAUSE");
return 0;
}


Thanks. EDIT: Wait, never mind about the time. Sorry. I just have the AND problem now.

##### Share on other sites
3D6 != 3 ~ 18.

3D6 would be
(rand() % 6) + (rand() % 6) + (rand() % 6) + 3;

With 3 dice, you have only one combination to his 3(1+1+1) but many to hit, for example, 12(4+3+5, 5+5+2, 6+1+5, etc).

If you change 3d6, to a 1d3~18, you make it an equal chance for a player to hit 3 or 12.

##### Share on other sites
I don't think I correctly understand what you're asking, but here's a response anyway. If anything I say is incorrect, someone please say so.

A while loop will only continue until one of them is false (so if any of the less-than's fail, the loop breaks.) So you need to just do this:
while(1){    if((STR == 18) && (IQ == 18) && (LUK == 18) && (DEX == 18) && (CON == 18) && (CHR == 18))        break;            STR = (rand() % 16) + 3;    IQ = (rand() % 16) + 3;    LUK = (rand() % 16) + 3;    DEX = (rand() % 16) + 3;    CON = (rand() % 16) + 3;    CHR = (rand() % 16) + 3;    cout << "STR:"<< STR << " IQ:" << IQ << " LUK:" << LUK << " DEX" << DEX <<" CON:" << CON << " CHR:" << CHR << "\n\n";    cout.flush();}

But this will run for awhile (most of the time).

##### Share on other sites
I don't understand the reason for the while loop. If you want to generate one set of stats, then there is no need for the while loop.

If you want to generate several sets, then why do you use that condition? The chance of all stats being 18 is 1 in 166. That means your program will generate 16,777,216 lines of text on average.

Also, I agree with Binomine: rand()%16+3 != 3d6

##### Share on other sites
Quote:
 Original post by JohnBoltonI don't understand the reason for the while loop. If you want to generate one set of stats, then there is no need for the while loop.If you want to generate several sets, then why do you use that condition? The chance of all stats being 18 is 1 in 166. That means your program will generate 16,777,216 lines of text on average.Also, I agree with Binomine: rand()%16+3 != 3d6

(I told you it would run for a while).

Here is my old stat generator:
template <UINT nFaces> UINT RollDie(UINT nTimes){	UINT nValue = 0;	for(UINT nCount = 0; nCount < nTimes; ++nCount)		nValue += rand() % nFaces + 1;	nValue += nTimes;	return nValue;}class StatsGenerator{public:	UINT m_nStrength;	UINT m_nDexterity;	UINT m_nIntelligence;	UINT m_nConstitution;	UINT m_nWisdom;	UINT m_nCharisma;	UINT m_nReserve;	void Roll()	{		m_nStrength	= RollDie<6>(3);		m_nDexterity	= RollDie<6>(3);		m_nIntelligence = RollDie<6>(3);		m_nConstitution = RollDie<6>(3);		m_nWisdom	= RollDie<6>(3);		m_nCharisma	= RollDie<6>(3);		m_nReserve	= 0;	}	UINT Shave(UINT nShaveTo)	{		if(m_nReserve)			m_nReserve = 0;		if(m_nStrength > nShaveTo)			m_nReserve += (m_nStrength - nShaveTo);		if(m_nDexterity > nShaveTo)			m_nReserve += (m_nDexterity - nShaveTo);		if(m_nIntelligence > nShaveTo)			m_nReserve += (m_nIntelligence - nShaveTo);		if(m_nConstitution > nShaveTo)			m_nReserve += (m_nConstitution - nShaveTo);		if(m_nWisdom > nShaveTo)			m_nReserve += (m_nWisdom - nShaveTo);		if(m_nCharisma > nShaveTo)			m_nReserve += (m_nCharisma - nShaveTo);		return m_nReserve;	}};

##### Share on other sites
I really don't mean to jump in and spoil your topic, but I was wondering if I could ask a question of my own regarding this. (I'm still a major newbie when it comes to the programming side of Game Development)

Many programming languges, other than BASIC and Python to some extent, so far have really confused me. Usually because I don't learn unless given an example to apply to. This looks like it might be one. I'm a big fan of DnD style RPGs like this, so lets assume that if I was making one, then the above code would be defined as a function? Yes? So for such a game I would define a function as say, diceRoll, or something and then determine XdY outside the function (E.g 3d6 or 2d4) and then call the function whenever a dice-roll was necessary? Just the whole function thing threw me off balance a bit.

Again I apologize for using your topic to answer my own question, but it just seemed relevant.

EDIT: Oh and that would also bring into play the whole global variables and whatnot right? Because then for the function to know what X and Y were, I'd have to make sure the variables were recognized by the function, which I'm not quite sure how to do anyways hehe.

##### Share on other sites
Quote:
 Original post by WoodlanderI'm a big fan of DnD style RPGs like this, so lets assume that if I was making one, then the above code would be defined as a function? Yes? So for such a game I would define a function as say, diceRoll, or something and then determine XdY outside the function (E.g 3d6 or 2d4) and then call the function whenever a dice-roll was necessary? Just the whole function thing threw me off balance a bit.

A function is a convenient package of code that does a single thing. In this case, yes, you could make a DiceRoll function. You would pass X and Y as parameters to the function, so that it knows which values to work with.

eg.
int DiceRoll(int x, int y){    int total = 0;    while (x > 0)    {        total += (rand() % y) + 1;        --x;    }    return total;}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

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

• Total Topics
634092
• Total Posts
3015448
×