mardi 20 octobre 2015

How does srand() work?

This is my code:

#include<stdio.h>
#include<stdlib.h>
#include <time.h>
int main(){
float m, n;
printf("Enter n, m:");
scanf("%f %f", &n, &m);
int l;
l=m-n;
int i;
for(i=0; i<4; i++){
    srand(time(NULL));
    double r=rand();
    r/=RAND_MAX;
    r*=l;
    r+=n;
    printf("%f ", r);
}
return 0;
}

Why it generates same numbers? and when I write srand(time(NULL));before the loop it generates different numbers! why it is like this? how does this program work?




Aucun commentaire:

Enregistrer un commentaire