generating random values

Sep 28, 2009 at 4:27am
hello guys

it's been a long time, i have a problem, i need to generate random values, but these values need to be coordinates (x,y) i would like to hear ideas about how to do it?

Thanks a lot for any suggestion
Sep 28, 2009 at 5:01am
Use rand().
Sep 28, 2009 at 5:03am
rand() % number
number is the max number you want
Sep 29, 2009 at 12:49am
Thanks for your answers but i think it is my fault not being more precisly!!!!

this is my problem

I generate a two dimension array static and then create ramdonly number but when i try to do the same thing using a dinamic array i got problems.. this is what i got

int Number_nodes;
int dimension;
int max_distance;
int* nodes;
int i,j;

printf("Please enter the number of nodes, dimension and maximum distance. ");
scanf("%d %d %d",&Number_nodes, &dimension, &max_distance);
nodes= (int *)malloc(sizeof(int)*Number_nodes);
for(j=0;j<Number_nodes;j++){
nodes[j]=(int *)malloc(sizeof(int)*Number_nodes);
}

for(i=0;i<Number_nodes;i++){
for(j=0;j<Number_nodes;j++){
nodes[i][j]=rand() % dimension;;
printf("File: %d Columm: %d %d\n",i,j,nodes[i][j]);
}
}
free(nodes);

Thanks beforehand for your help!!!!
Last edited on Sep 29, 2009 at 12:55am
Sep 29, 2009 at 12:56am
use srand() to seed your random.
also, use code brackets so that

int main()

becomes
 
int main()

Last edited on Sep 29, 2009 at 12:57am
Topic archived. No new replies allowed.