0

I'm just starting to learn C++ and I can't figure out why the rand() is not working here for the rect0 object. When I run the program, it doesn't randomly draw rect0 as I thought it would. It just prints out at the same location every time. Here's the code:

class Rect
{
    Vec2 min, max;
public:
    Rect(int minx, int miny, int maxx, int maxy)
        :min(minx, miny), max(maxx, maxy)
    {}
    Rect() {}
    void draw(const char letter) const
    {
        for (int row = min.y; row < max.y; row++)
        {
            for (int col = min.x; col < max.x; col++)
            {
                if (row >= 0 && col >= 0)
                {
                    moveCursor(col, row);
                    putchar(letter);
                }
            }
        }
    }
        void setRandom(Rect & r)
    {
        r.min.x = rand() % 5;
        r.max.x = rand() % 10 + 10;
        r.min.y = rand() % 2;
        r.max.y = rand() % 10 + 10;
    }
int main()
{
    Rect rect0;
    rect0.setRandom(rect0);
    rect0.draw('0');
    moveCursor(0, 0);   // re-print instructions
    return 0;
}
brisk172
  • 15
  • 1
  • 2
  • Read this [http://www.cplusplus.com/reference/cstdlib/srand/](http://www.cplusplus.com/reference/cstdlib/srand/) – Logman Nov 07 '16 at 03:28
  • Don't use it. [Use `std::uniform_int_distribution` instead.](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) – user4581301 Nov 07 '16 at 07:23

0 Answers0