jeudi 18 janvier 2018

strange behavior of rand ()

I have write this code for resolve a college assignment but I have one problem with the rand() function.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int dim = 9;

bool cRiga(int schema[][dim], int riga, int n);
bool cCol(int schema[][dim], int col, int n);
bool cQuad(int schema[][dim], int x, int y, int n);
void riempi(int schema[][dim]);
void stampa(int schema[][dim]);

int main()
{
    srand(time(0));
    int schema[dim][dim] = { { 0 } };
    riempi(schema);
    cout << endl;
    stampa(schema);

}

bool cRiga(int schema[dim][dim], int riga, int n)
{
    for (int i = 0; i < dim; i++)
    {
        if (schema[riga][i] == n)
        {
            return false;
        }
    }
    return true;
}

bool cCol(int schema[dim][dim], int col, int n)
{
    for (int i = 0; i < dim; i++)
    {
        if (schema[i][col] == n)
        {
            return false;
        }
    }
    return true;
}

bool cQuad(int schema[][dim], int x, int y, int n)
{
    x = x - (x % 3);
    y = y - (y % 3);
    for (int i = x; i < x + 3; i++)
    {
        for (int j = y; j < y + 3; j++)
        {
            if (schema[i][j] == n)
            {
                return false;
            }
        }
    }
    return true;
}

void riempi(int schema[dim][dim])
{
    int n;
    for (int i = 0; i < dim; i++)
    {
        for (int j = 0; j < dim; j++)
        {
            while (true)
            {
                n = rand() % 9 + 1;

                // if (cRiga(schema, i, n)) { // controllo ISOLATO sulla riga: OK!
                // if (cCol(schema, j, n)) { // controllo ISOLATO sulla colonna: OK!
                // if (cQuad(schema, i, j, n)) { // controllo ISOLATO sul quadrato: OK!

                // va in loop con un range inferiore a 1-11
                // if (cRiga(schema, i, n) && cCol(schema, j, n)) {

                // va in loop con un range inferiore a 1-15
                if (cRiga(schema, i, n) && cCol(schema, j, n)
                        && cQuad(schema, i, j, n))
                {
                    schema[i][j] = n;
                    cout << i << "," << j << endl;
                    break;
                }
            }

        }
    }
}

void stampa(int schema[][dim])
{
    for (int i = 0; i < dim; i++)
    {
        for (int j = 0; j < dim; j++)
        {
            cout << schema[i][j] << " ";
            if ((j + 1) % 3 == 0 && j != 0)
            {
                cout << " ";
            }
        }
        if ((i + 1) % 3 == 0 && i != 0 && i != dim - 1)
        {
            cout << endl;
        }
        cout << endl;
    }
}

At line 59 I have:

rand()% 9 + 1

if I replace 9 with a value greater than 10, the row and column controls work together. if instead I replace 9 with a value greater than 15 everything works correctly.

Why if I leave the

rand() % 9 +1 

only one control works at a time? If I try to combine 2 together it goes in loop.




Aucun commentaire:

Enregistrer un commentaire