mardi 9 mai 2017

rand() give the same result in loop

I have a random function that gives numbers according to possibilities of some intervals.

int myrand (float array[]){ //this function gives an interval according to 
float possib[part];        // possibilities
int i;
float r;
possib[0]=array[3];
for (i=1;i<part;i++) possib[i]=possib[i-1]+array[i+3]; 
r=(float)rand()/32767;
printf(" r= %f ",r); //this is to check whether r is different in every run
for(i=0;i<part;i++){
    if(r<possib[i]){
        return i;
        break;
    }
}   

}

double myrandfinal(float array[],int n){ //this function gives a random 
double final;  //double in the chosen interval
    final=array[n]+(((float)rand()/32767)*(array[n]-array[n-1]));
    printf("final= %f",final-array[n]); //again to check
    return final;

and then i have an evaluate function that makes evaluations with the chosen number (final). In main i have something like the following:

int main(){
int i;
srand(time(NULL));
for(i=0;i<10;i++)
printf("%f",eval()); //the evaluate function
}

Although i get different number in all checks, the result of the evaluate function is the same. In main when i just wrote printf("%f",eval()); without the loop and execute myself for a couple times,i get different results. What would be the reason?




Aucun commentaire:

Enregistrer un commentaire