0

i am trying to create random data so i can review an algorithm in cache policy. When i use the first code with rand() i get the same numbers every time i run my code(i knew this was coming) so i tried to use srand. when i inserted it i get different numbers every time i run the code but the same 10 numbers at my loop. What am i doing wrong to create random data??? After that i will make with zip ununiformed data. sorry my bad the loop is in main function:

for(counter=0; counter<10; counter++){
    printf("Number %d is %lf\n", counter, rand_val());
}

And i am using this function to create numbers.

double rand_val(){

double rnum;

srand(time(NULL));
rnum=rand();

return(rnum);
}
P.George
  • 11
  • 3
  • What loop? Oh wait, are you calling this in a loop? You're supposed to call the srand once at the start of your program. Otherwise you'll only get a fresh random number every second... –  Dec 11 '20 at 17:48
  • Sorry my bad the loop is in main function and forgot to write it. – P.George Dec 11 '20 at 17:54
  • Thanks my friend i see i should have put the srand in the main to run only once!! – P.George Dec 11 '20 at 17:55

0 Answers0