-3

I would like to create a matrix which has all it's elements as either 1 or 0. This is what I've tried, it produces a matrix with 1s and 0s at random locations just as I want. However, it produces the same matrix everytime.

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

int a[10][10];
int i = 0,j= 0,n=0;

int main(void)
{
 populate(5);
 print(5);

 return 0;
}

int populate(int n)
{
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   a[i][j] = rand()%2;
  }
 }
}

int print(int n)
{
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("%d ",a[i][j]);
  }
  printf("\n");
 }
}

EDIT : Based on replies, I have edited my function as follows. However I see that my matrix is now all zeros or all ones.

int populate(int n)
{
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   srand (time(NULL));
   a[i][j] = rand()%2;
  }
 }
}
tubby
  • 2,074
  • 3
  • 33
  • 55
  • "Anyone who believes an algorithm can generate randomness is existing in a state of mathematical sin." Or at least, pain. see this: http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand?rq=1 you need to reseed `rand` – cphlewis Jun 18 '15 at 03:42
  • 1
    If you edited your function that way, you didn't do it based on replies. For example, you seem to have completely missed this, "For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand." and this "Add srand(time(NULL)); at the start of main." – David Schwartz Jun 18 '15 at 04:10

2 Answers2

1

However, it produces the same matrix everytime.

That's because you haven't added the code to seed the random number generator. Add

srand(time(NULL));

at the start of main, before any calls to rand() are made.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You need to add srand ( time(NULL) );

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

Here is an example how you seed srand:

/* srand example */
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
  printf ("First number: %d\n", rand()%100);
  srand (time(NULL));
  printf ("Random number: %d\n", rand()%100);
  srand (1);
  printf ("Again the first number: %d\n", rand()%100);

  return 0;
}

EDIT: As the OP edited the question, here is the solution: Use srand(time(NULL)); very beginning of the method.

int populate(int n) {
    srand(time(NULL));
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            a[i][j] = rand() % 2;
        }
    }
    return 0;
}
mazhar islam
  • 5,561
  • 3
  • 20
  • 41