When I run the code, the rand()
function seems to generate the same non-random numbers (my intention is to generate random numbers in value
). If instead of rand()
I use the formal parameter length
, it seems that the code works (getting decreased numbers). Where am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node{
int value;
struct node *next;
};
struct node *construct(int);
int main(){
struct node *list = construct( 5 );
while( list ){
printf(" %i\n", list->value);
list = list->next;
}
return 0;
}
//It builds a list of "length" items recursively
struct node *construct(int length){
struct node *node = (struct node *) malloc(sizeof(struct node));
srand(time(NULL));
node->value = rand(); /* unclear part of code */
if( length - 1){
node->next = construct( length - 1 );
}
return node;
}
/* missing the code to free memory */
Aucun commentaire:
Enregistrer un commentaire