2

When I execute this code, I don't get a matrix of completely random numbers but instead get many repeating numbers. Any idea how I can rainGen function to produce a truly random 2d matrix? Any help you can provide would be really really appreciated!!

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

const int MATRIX_ROW = 5;
const int MATRIX_COL = 5;

int** rainGen(int **theMatrix_ptr_ptr, int rows);

int main()
{
    int matrix[MATRIX_ROW][MATRIX_COL];
    int *matrix_ptr[MATRIX_ROW];
int **matrix_ptr_ptr = &matrix_ptr[0];
int rows = 3;

for (int count = 0; count < MATRIX_ROW; count++)
    matrix_ptr[count] = &matrix[count][0];

rainGen(matrix_ptr_ptr, rows);

return 0;
}

int** rainGen(int **theMatrix_ptr_ptr, int rows)
{
    srand(1023);

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < MATRIX_COL; j++)
            *((*theMatrix_ptr_ptr + i) + j) = rand() % 100;

return theMatrix_ptr_ptr;
}
1
  • If you are using C++ as is apparent, better use new functions available in <random>, like default_random_engine, uniform_int_distribution, and etc. Commented Mar 25, 2013 at 5:15

4 Answers 4

1

I added some debugging code. The int matrix[MATRIX_ROW][MATRIX_COL]; statement allocates memory for matrix but it doesn't initialize the element values. The values will be whatever bit pattern was left from the previous memory use. I initialized the matrix element values to an unexpected value of -1. At the end, I printed the matrix.

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

const int MATRIX_ROW = 5;
const int MATRIX_COL = 5;

int** rainGen(int **theMatrix_ptr_ptr, int rows);

int main()
{
     int matrix[MATRIX_ROW][MATRIX_COL];
     // initialize matrix elements to -1.
     for (int i = 0; i < MATRIX_ROW; ++i)
          for (int j = 0; j < MATRIX_COL; ++j)
               matrix[i][j] = -1;
     int *matrix_ptr[MATRIX_ROW];
     int **matrix_ptr_ptr = &matrix_ptr[0];
     int rows = 3; // error
     for (int i = 0; i < MATRIX_ROW; i++)
          matrix_ptr[i] = &matrix[i][0];
     rainGen(matrix_ptr_ptr, rows);
     // print matrix elements.
     for (int i = 0; i < MATRIX_ROW; ++i) {
          for (int j = 0; j < MATRIX_COL; ++j)
               cout << " [" << i << "," << j << "] " << matrix[i][j];
          cout << endl;
     }
     return 0;
}

int** rainGen(int **theMatrix_ptr_ptr, int rows)
{
     srand(1023);
     for (int i = 0; i < rows; i++)
          for (int j = 0; j < MATRIX_COL; j++)
               *((*theMatrix_ptr_ptr + i) + j) = rand() % 100; //error
     return theMatrix_ptr_ptr;
}

Output:

[0,0] 8 [0,1] 53 [0,2] 30 [0,3] 51 [0,4] 40 
[1,0] 88 [1,1] 60 [1,2] -1 [1,3] -1 [1,4] -1 
[2,0] -1 [2,1] -1 [2,2] -1 [2,3] -1 [2,4] -1 
[3,0] -1 [3,1] -1 [3,2] -1 [3,3] -1 [3,4] -1 
[4,0] -1 [4,1] -1 [4,2] -1 [4,3] -1 [4,4] -1 

As you can see from the matrix elements with a value of -1, you are not giving all the elements of the matrix a random value.

As a helpful hint, I marked two statements of particular interest with an // error comment.

Once you have fixed your problems, the output should look something like this:

[0,0] 8 [0,1] 14 [0,2] 19 [0,3] 36 [0,4] 99 
[1,0] 53 [1,1] 45 [1,2] 20 [1,3] 64 [1,4] 3 
[2,0] 30 [2,1] 51 [2,2] 40 [2,3] 88 [2,4] 60 
[3,0] 22 [3,1] 36 [3,2] 42 [3,3] 1 [3,4] 49 
[4,0] 96 [4,1] 24 [4,2] 48 [4,3] 92 [4,4] 46 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should put your srand() out of the function

int** rainGen(int **theMatrix_ptr_ptr, int rows)

you should put it into main().

Another method :

You can change srand(1234) to srand(time())

but you must

#include <time.h>

first

Comments

0

srand() reinitialize the random generator. you can use current time as a parameter of srand(). that will solve your proble

Comments

0

According to my understaning of your question. I have provided some code for you. May be this will help to you.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    --------->int <---------
    main (int argc, char *argv[])
    {
      /* Simple "srand()" seed: just use "time()" */
      unsigned int iseed = (unsigned int)time(NULL);
      srand (iseed);

      /* Now generate 5 pseudo-random numbers */
      int i;
      for (i=0; i<5; i++)
      {
        printf ("rand[%d]= %u\n",
          i, rand ());
      }
      return 0;
    }

 /*
     * rand: Generates 5 numbers using standard "srand()/rand()" function
     *
     * SAMPLE OUTPUT:
     *   rand[0]= 824522256
     *   rand[1]= 1360907941
     *   rand[2]= 1513675795
     *   rand[3]= 1046462087
     *   rand[4]= 253823980
     */

Comments

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.