dimanche 6 novembre 2016

C++ Double random number (0,1) exluding 0,1 for school

im trying to make code that stores random double numbers from interval (0 to 1) excluding 0 and 1. So far i made this but it apparently is for interval from 0 to 1 including 0 and 1. That IF condition is there because i didn't know how to exclude the 1 and 0 so the cycle goes back by 1 to generate new number. How can i modify this function to correctly generate from said interval? We dont use C++ 11 at school so please just tell me how to modify the generating line. generating seed is at the main function.

void init(double *A, const int N)
{  
double p;

for(int i=0; i<N; ++i)
{
  //  p = (((double)rand() / RAND_MAX) + 1) / (((double)(RAND_MAX))+2);
     p = (double)rand() / RAND_MAX;
    if(p == 0 || p == 1)
    {
        i--;
        continue;
    }
   else A[i]= p;
    }
}




Aucun commentaire:

Enregistrer un commentaire