2

Alright so in Class A I have:

int* attackerDice;
int* defenderDice;

these two variables call on another class called dice. So:

attackerDice = dice.roll()
defenderDice = dice.roll()

what dice.roll does is the following:

int* Dice::roll () {
    int* ran = new int[1];
    srand(time(NULL));
    ran[0] = rand()%6+1;
    std::sort(ran,ran+1);
    return ran; 
}

Now the problem that I am getting is attackerDice is returning -43249211234 while defender dice is returning the right number. I assume this is because I clearly do not understand when to use * and &. Can anyone help me out?

Edit: How would I go about doing this if I wanted to have an array? Id like to have it so that I can roll X number of dice

4
  • 1
    Why are you sorting an array with 1 element? Commented Oct 11, 2015 at 20:02
  • 1
    Why are you dynamically allocating a single int? That uses more memory, will be error prone, and is slower than just returning an int. Why are you sorting an array with a single element? Commented Oct 11, 2015 at 20:03
  • std::sort(ran,ran+1); Huh? What's the purpose of this? You are trying to sort an array with a single element? Commented Oct 11, 2015 at 20:04
  • Oops. Sorry I was implementing it so that I could roll as many dice as I want and it would return them from largest to smallest. I wasn't finished though. Commented Oct 11, 2015 at 20:43

2 Answers 2

3

My crystal ball says that Dice doesn't obey Rule Of Three.

It looks like you're having Undefined Behaviour because of mismanaged pointers and the lifetime of the objects they are pointing to.

In most cases, C++ doesn't require the use of pointers anyways.

Don't Write new In C++

See Jeffrey's answer for an idea how to avoid the lifetime issues.

Much better. Caution: It's very suboptimal to instantiate a new random generator each time through the loop, and using a uniform distribution like that is flawed. Instead, do:

auto dice = bind(uniform_int_distribution<>(1,6), mt19337 { random_device{}() });

Now you can just call

int roll1 = dice(); // roll1 = [1..6]

And it will actually be uniformly distributed as long as you use the same dice object

Sign up to request clarification or add additional context in comments.

3 Comments

That single line dice object is pretty clever.
@ʎǝɹɟɟɟǝſ Well the dice thing can be found on: cplusplus.com/reference/random. The real problem is here is that the singular for dice is actually die, and the function should read int roll1 = die().
Hahaha. That was pretty funny
2

Pretty much everything about your code is suboptimal:

  • You are using dynamic allocation when there's really no need.
  • You are allocating an array of a single element and sorting it (which makes no sense by the way).
  • You are using rand which has been considered "evil" for quite some time now.
  • You are putting an algorithm that does not use any member object of the class Dice as one of its member functions.

A revised "sane" solution would be the following:

namespace ex {
    class dice {
    private:
        std::mt19937 gen;
        std::uniform_int_distribution<> dis;
    public:
        dice(int to)
            : gen(std::random_device()())
            , dis(1, to)
            {}

        int roll() { return dis(gen); }
    };
}

which would then be used as something like:

constexpr int count = 10;
std::vector<int> rolls;
rolls.reserve(count);

ex::dice dice(6);
std::generate_n(back_inserter(rolls), count, std::bind(&ex::dice::roll, dice));

Live demo

3 Comments

Very good notes. I refer to your example from my answer. However, there's some issues with using <random> like that remaining. +1 for the explanations though.
Thanks a lot ʎǝɹɟɟɟǝſ. I'm still very new to C++ so I had no clue rand was considered "evil". Ive been mainly programming in java up until now so I'm still learning the language.
Is it at all possible to nicely do this with an int array instead of a regular int? So that I can end up rolling multiple dice at one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.