mardi 10 mai 2016

Stuck on this task for 12 hours, On the verge of giving up [on hold]

we have taken 30 nodes in a contiki cooja simulator. The program creates a dat file where time remains constant for a long time. We want time to be changing frequently and randomly with respect to x y axis.. Pleas help me. Please suggest way to make time random.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//Random number dihunnanwi horgra function.
float rand_int(float a,float b) 
{
//The basic function here (rand() % range)+ Lower_bound

   float c;
   c=rand();


if(a<0 && b<0)
{

    if (a > b)
        return(-(fmod(c,(a-b+1)) + b));
        else if (b > a)
        return(-(fmod(c,(b-a+1)) + a));
    else 
        return a;

}

else if(c<0 && (b<0 || a<0))
{
    if (a > b)
        return(-(fmod(c,(a-b+1)) + b));
        else if (b > a)
        return(-(fmod(c,(b-a+1)) + a));
    else 
        return a;
}
else if(a>0 && b>0)
{
    if (a > b)
        return(fmod(c, (a-b+1)) + b);
        else if (b > a)
        return(fmod(c,(b-a+1)) + a);
    else 
        return a;
}
else
{
    if (a > b)
        return(fmod(c, (a-b+1)) + b);
        else if (b > a)
        return(fmod(c, (b-a+1)) + a);
    else 
        return a;
}   


}


int main(){
FILE *fp;

int j, nodes, flag1,flag2;
float r1, r2;
double i, x[50], y[50], time ,lim1, lim2;

fp=fopen("positions.dat","w");
printf("\nEnter the number of nodes: ");
scanf("%d",&nodes);
printf("\nEnter the time period of your simulation: ");
scanf("%lf",&time);
i=0;
flag1=0;
flag2=0;

//Initial node position fwrni takai.
for(j=0; j<nodes; j++)
{

   do
   {
       r1=rand();
   }while(r1<0 || r1>200);
   do
   {
   r2=rand();
   }while(r2<0|| r2>200); 

 x[j]=r1;
 y[j]=r2;


}



while(i <= time)
{

for(j=0; j<nodes; j++)
{


    //File aao write kalamgra function
    fprintf(fp,"%d %lf %lf %lf\n",j,i,x[j],y[j]);


    //Mote fwrni random positions
    //Random babw range hwjanai, baradrai difference jaywini takai.
    if(flag1==0)
    x[j]=rand_int(x[j],x[j]+1.5);
    else if(flag1==1)
    x[j]=rand_int(x[j]-1.5,x[j]);
    else
    x[j]=rand_int(x[j]-0.75,x[j]+0.75);

    if(flag2==0)
    y[j]=rand_int(y[j],y[j]+1.5);
    else if(flag2==1)
    y[j]=rand_int(y[j]-1.5,y[j]);
    else
    y[j]=rand_int(y[j]-0.75,y[j]+0.75);


    //motes area (-ve 300 nifrai +ve 300 sim) areakwu fisa kalamnw valuekwu       komai.
    if(x[j]>=200)
        flag1=1;
    else if(x[j]<=0)
        flag1=0;
    else
    {}
    if(y[j]>=200)
        flag2=1;
    else if(y[j]<0)
        flag2=0;
    else
    {}
}


i=i+0.20;   //Random kalamnw value swlai.... (Greater value->Lower   randomness)
}
fclose(fp);
return 0;
}




Aucun commentaire:

Enregistrer un commentaire