I noticed that if I used rand()
to generate random values inside a loop, it will change the value at each iteration. However, this is not the case between for function invocations even by seeding via srand()
.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
void test(){
float r;
srand(time(NULL));
r = (double)rand()/(double)RAND_MAX;
printf(" %6.3f \n",r);
}
int main(){
float r;
//srand(time(NULL));
for(int i=0;i<5;i++){
test();
//r = (double)rand()/(double)RAND_MAX;
//printf(" %6.3f \n",r);
}
}
How can I force rand() to refresh its values in each function call?