13
for(i = 0; i < n; i++){
        srand(time(NULL));
        printf("%d ", time(NULL));
        for(j = 0; j < (n-1); j++){
            a[i][j] = rand();
        }
    }

I try to generate random numbers, but they are the same... I try srand(i * time(NULL)). No matter.. What should i do?

Array declaration:

int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);

a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
    a[i] = (int*)calloc(n-1, sizeof(int));
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Sergey Gavruk
  • 3,538
  • 2
  • 20
  • 31
  • 2
    Your first `calloc` call should be `sizeof(int *)` but you seem to be working on a host where simple pointers and ints are the same size (that's true of most architectures). – mpez0 Mar 16 '10 at 00:33
  • You essentially edited your question right after answers were given, right in a way that these answers don't match the question any more and got invalidated this way. Please don't do so. – Aconcagua Oct 18 '18 at 08:01

10 Answers10

21

Call srand() outside of the loop. You are reseeding it every iteration.

srand() seeds the random number generator so you get a different sequence of random numbers depending on the input. Your loop runs very fast, so the call to time(NULL) always returns the same value. You are resetting to the same random sequence with every iteration. As a general rule, only call srand() once in your program.

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • Agreed, you (generally) shouldn't need to seed a random number generator more than once. – bta Mar 15 '10 at 18:28
  • @bta: true that you shouldn't need to seed `rand()` more than once. Re-seeding "proper" random number generators, by which I mean anything used for security, is another matter. – Steve Jessop Mar 15 '10 at 18:37
6

Don't call srand() every time through the loop - just do it once beforehand.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
4

FAQs 13.15 to 13.20 will be of interest. And I am tempted to create a new tag for such questions.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • Maybe create a single question titled "how does srand() affect the state of the random number generator?", and mark it as a duplicate each time another subtle variation comes up ;-) – Steve Jessop Mar 15 '10 at 18:39
  • Yes, a CW version. But then we'd be duplicating all the comp.lang.*.faqs, won't we? – dirkgently Mar 15 '10 at 18:45
  • 1
    Yes, but SO will inevitably contain myriad duplicates of FAQs (by definition of F), so will duplicate comp.lang.*.faq whatever you do. Unless questions are ruthlessly marked as duplicates, though, even when they vary the theme a bit, they'll end up with less information than the existing FAQ (assuming the FAQ covers it well). The CW version of FAQs which are also F on SO could be tagged as such, and link to the language FAQ and any other relevant resources, and could phrase the question in the most general way, to ensure they catch the most dupes. Or something. – Steve Jessop Mar 15 '10 at 20:12
  • Something, I too have been thinking on. Move this discussion to meta? – dirkgently Mar 15 '10 at 20:39
3

srand is a function that "seeds" the random number generator. In case you don't know, random numbers in computers aren't really random. In effect, the computer just has a list of numbers that seem random in it, and you use srand to tell it where to start in that list, with each call to rand() returning the next item in the list.

The reason you write srand(time(NULL)) is to get the random numbers to start at some point that isn't going to be the same every time you run the program (unless the programs start at the same time).

So what you are doing here is repeatedly telling the program to restart the random number list at the same point (because the time is the same each time you go through the loop). Move the call to srand outside the loop and you will get the correct results.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
3

alt text

Community
  • 1
  • 1
kenj0418
  • 6,688
  • 2
  • 27
  • 23
2
srand(time(NULL)); 

for(i = 0; i < n; i++){         
        printf("%d ", time(NULL)); 
        for(j = 0; j < (n-1); j++){ 
            a[i,j] = rand(); 
        } 
    } 

Call srand once outside the loop.

JonH
  • 32,732
  • 12
  • 87
  • 145
1

You need to call srand() before entering the loop. srand() initializes the radnom number generator with the given seed and generates unique sequence of random numbers for this seed.

Your loop executes very fast so every call to time(NULL) yields the same time (measured in seconds) - hence you initialize random number generator with the same seed on every loop iteration.

pajton
  • 15,828
  • 8
  • 54
  • 65
0
srand(time(NULL));
for(i = 0; i < n; i++){
    for(j = 0; j < (n-1); j++){
        a[i,j] = rand();
    }
}

No matter. The number are the same...

Sergey Gavruk
  • 3,538
  • 2
  • 20
  • 31
  • What is this `a[i,j]` syntax? You want `a[i][j]`, probably, although you should be getting a compiler error unless j is a plain array instead of 2-D array like I'm sure you intend. – indiv Mar 15 '10 at 18:50
0
int** a;
int i;
printf("Enter array size: ");
scanf("%d", &n);
if( n < 1 ){
    printf("Size should be > 0\n\n");
    return NULL;
}
a = (int**)calloc(n, sizeof(int));
for(i = 0; i < n; i++)
    a[i] = (int*)calloc(n-1, sizeof(int));

Here is my array...

Sergey Gavruk
  • 3,538
  • 2
  • 20
  • 31
  • 1
    First, you should edit your post to add this new information. This site doesn't work like a forum. Second, I stand by my statement that your 2-D array syntax is wrong, and you should do `a[i][j]` when accessing an item. Here's a multi-dimensional array tutorial: http://www.functionx.com/cpp/Lesson12.htm – indiv Mar 15 '10 at 19:06
0

Sergey, you did not get an error message with the a[i,j] version simply because this is a perfectly valid expression. The comma operator evaluates the sub-expressions from left to right and returns the value of the last expression. Thus, writing a[i,j] is identical to a[j]. What you received in the print was the value of the pointer to the j-th vector in your matrix.

ysap
  • 7,723
  • 7
  • 59
  • 122