I'm trying to generate numbers between -1 and 1 using the rand() function so i can pass random values to the two members of N structs but the program prints the same values N times. Any advice?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Complex
{
double x;
double y;
};
int main ()
{
int N,i,j,k;
struct Complex *comp;
printf("Enter N:\n");
scanf("%d",&N);
comp = malloc(N*sizeof(struct Complex));
if (comp == NULL)
{
printf("Error! Out of memory");
return 1;
}
for (j=0;j<N;j++){
srand ( time ( NULL));
(comp + j)->x = (double)rand()/RAND_MAX*2.0-1.0;
(comp + j)->y = (double)rand()/RAND_MAX*2.0-1.0;
}
for (i=0; i<N; i++)
{
printf("\n%f\t%f",(comp + i)->x,(comp + i)->y);
}
return 0;