Repeating Similar Code with Different Results

Howdy!
I'm currently in the process of making a fast character generator for D&D DMs. Thus far, I've gotten the code to generate the numbers that one would need to roll for a stat. Afterward, the lowest is detected and set to NULL before the rest are added and displayed. Thus, I've successfully gotten the value for Strength.

However, I've had difficulty repeating the code after the first stat. As you can see below: I tried to generate an entirely different set of float integers for the next stat: Dexterity. My thinking was that by making a random number set between 5 and 8, rather than 1 and 4, as well as separate values for the total (totDEX) and lowest number (lowest2) it would generate and display those different values on the next line.

While the next line displays just fine, the Dexterity is always a repetition of the Strength value.

I've considered using functions or a loop of some variety to solve this, but having re-read the tutorials, I don't think that they're the solution.
I also tried starting the second set of lines with another int main() {, as well as a corresponding } at the bottom, but that only both crashes the program in addition to it running normally.

Any assistance rendered would be wildly appreciated.

Also, I've referenced an article for properly inputting code in the post. It should work, but due to the preview button displaying a blank box, I won't be sure until it's up. Thank you for your patience in my previous post(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>

using namespace std;

int main() {
	srand((unsigned) time(0));
	float RN1, RN2, RN3, RN4, totSTR; //Each of these values is created...
	for (int index = 0; index < 1; index++) {
		RN1 = (rand() % 6) + 1;
		RN2 = (rand() % 6) + 1;
		RN3 = (rand() % 6) + 1;
		RN4 = (rand() % 6) + 1;
		int lowest;
		lowest = NULL;
		if (RN1 < RN2, RN3, RN4) RN1 = lowest; //...and are sorted for the lowest here.
		else if (RN2 < RN1, RN3, RN4) RN2 = lowest;
		else if (RN3 < RN1, RN2, RN4) RN3 = lowest;
		else if (RN4 < RN1, RN2, RN3) RN4 = lowest;
		totSTR = RN1 + RN2 + RN3 + RN4; //Here, the four values, and the lowest which is now 0, are added...
		cout << "Your Strength is: " << totSTR << endl;//and displayed here...
		srand((unsigned)time(0)); //Here, I've repeated the previous code with a separate set of float values.
			float RN5, RN6, RN7, RN8, totDEX;//I figured that by setting up a new set of values altogether, they'd generate differently.
			for (int index = 0; index < 1; index++) {
				RN5 = (rand() % 6) + 1;
				RN6 = (rand() % 6) + 1;
				RN7 = (rand() % 6) + 1;
				RN8 = (rand() % 6) + 1;
				int lowest2;
				lowest2 = NULL;
				if (RN5 < RN6, RN7, RN8) RN5 = lowest2;//They're executed in a repeat of the same code, but being a different set of values, and that includes another int lowest.
				else if (RN6 < RN5, RN7, RN8) RN6 = lowest2;
				else if (RN7 < RN5, RN6, RN8) RN7 = lowest2;
				else if (RN5 < RN6, RN6, RN7) RN8 = lowest2;
				totDEX = RN5 + RN6 + RN7 + RN8;
				cout << "Your Dexterity is: " << totDEX << endl; //However, it displays the same number as the four different values above. This says that they're creating the exact same number values (RN1 always = RN5).
			}
		}
	}
//But how? 
I tried to generate an entirely different set of float integers for the next stat: Dexterity.

Why are you using floating point numbers? I thought that D&D stats are based on dice rolls, which would seem to indicate integers.

Why are each of the die rolls separate variables? Wouldn't a vector make more sense?

While the next line displays just fine, the Dexterity is always a repetition of the Strength value.

By the way you should only call srand() once, calling it twice is probably why the numbers appear to be repeating.


I've considered using functions or a loop of some variety to solve this, but having re-read the tutorials, I don't think that they're the solution.

Well a function would certainly simplify the logic and reduce/remove most of your code duplication. Functions should be your friend, they will greatly simplify things.

I also tried starting the second set of lines with another int main()

In C++ you can have one and only one main().


This is incorrect syntax:

 
if (RN1 < RN2, RN3, RN4)

Presumably you mean

1
2
if (RN1 < RN2 && RN1 < RN3 && RN1 < RN4)
    RN1 = 0;   // is this what you mean my RN1 = lowest??? 

Don't set an int to NULL (which is C's NULL pointer). Use 0 if you mean 0.

You seem to want to roll four dice, toss out the lowest one, and sum the remaining three.
How about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <random>

// return sum of num_dice rolls minus the minimum value
int roll(int num_dice = 4)
{
    static std::default_random_engine rnd(std::random_device{}());
    static std::uniform_int_distribution<int> dist(1, 6);

    int min = dist(rnd), sum = min;
    for (int i = 1; i < num_dice; ++i)
    {
        int r = dist(rnd);
        sum += r;
        if (r < min) min = r;
    }

    return sum - min;
}

int main()
{
    for (int i = 0; i < 10; ++i)
        std::cout << roll() << '\n';
}

Topic archived. No new replies allowed.