Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
class Player {

    public:
        int d6_1;
        int d6_2;
        int d6_3;
        int d6_4;
        int d6_5;
        int d6_6;
        double pi = 2 * acos(0.0);
        string playerClass;
        string name;
        string race;
        string size;
        int strength = d6_1;
        int dexterity = d6_2;
        int constituion = d6_3;
        int intelligence = d6_4;
        int wisdom = d6_5;
        int charisma = d6_6;
        //Abilites Mods'
        int strMod = floor((strength - 10) / 2);
        int dexMod = floor((dexterity - 10) / 2);
        int conMod = floor((constituion - 10) / 2);
        int wisMod = floor((wisdom - 10) / 2);
        int intMod = floor((intelligence - 10) / 2);
        int chaMod = floor((charisma - 10) / 2);
        string meleeWeapon = "fists";
        string rangedWeapon;
        string magic;
        
        void statAssign() {
            for (int i = 0; i < 7; i++) {
                srand(time(NULL));
                int d6 = (rand() / pi);
                switch(i) {
                    case 1:
                        d6 = d6_1;
                        d6_1 = abs(d6_1%6+1);
                        d6=0;
                        break;
                    case 2:
                        d6 = d6_2;
                        d6_2 = abs(d6_2%6+1);
                        d6=0;
                        break;
                    case 3:
                        d6 = d6_3;
                        d6_3 = abs(d6_3%6+1);
                        d6=0;
                        break;
                    case 4:
                        d6 = d6_4;
                        d6_4 = abs(d6_4%6+1);
                        d6=0;
                        break;
                    case 5:
                        d6 = d6_5;
                        d6_5 = abs(d6_5%6+1);
                        d6=0;
                        break;
                    case 6:
                        d6 = d6_6;
                        d6_6 = abs(d6_6%6+1);
                        d6=0;
                        break;
                }
        }
    }
};

Above this is my player class. As janky as it is it has one problem at the moment and that is the fact that the random number generator limit is not working. If more of the code is needed just ask but other than that if anymore has the time to look at this and give me a solution then thanks a ton. Oh and I'm new so probably alot of rookie mistakes.

What I have tried:

Multiple sources, including documentation.
Posted
Updated 3-Aug-23 19:59pm

See this article C library function - srand() | Tutorialspoint[^]

After you seed the generator as you have done, you set the limit as below;

C++
printf("%d\n", rand() % 50);


This sets upper limit to 50.
 
Share this answer
 
v2
I'll point out one obvious thing : you should not repeatedly seed the PRNG. It is best to seed it just once. However and this is important : if you want a repeated sequence of random numbers then you have to use the same seed. That gives you the option to use a random sequence or the same sequence again. That can be a very handy thing and I have taken advantage of it in the past.

As Mike mentioned, you use the modulus operator to limit the values. Using a value of 50 with it will give you a range of values between 0 and 49.

A while ago I wrote a function to generate random numbers within an arbitrary, inclusive range. You might find it helpful so here it is :
C++
int GetRandomValue( int minval, int maxval )
{
    int range = maxval - minval + 1;
    int rv = rand() % range;
    return rv + minval;
}
Note the plus one in the range calculation. That's because the range needs to be inclusive. If you want a range of 0 to 50 then you have to use 51 with the modulus operator.

Here's an interesting learning experiment to perform : draw a histogram that show the distribution of values generated by srand.
 
Share this answer
 
You may also have a look at std::random.
The documentation (https://cplusplus.com/reference/random/uniform_int_distribution/[^]) gently provides sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900