mercredi 22 juin 2016

How to generate Random floating point digits precise upto 6 point decimals?

I wanted to know how to generate Random floating point digits precise upto maximum 6 point decimals and within the range of (-999 to +999) in C language

I tried with:

unsigned int i, num;
int divide;
FILE *f;

f=fopen("/dev/urandom", "r");
if (!f) {
        fprintf(stderr, "Failed open file\n");
        exit(1);
    }
for(i=0; i< w*w; i++)
{
    fread(&num, sizeof(unsigned int), 1, f);
    fread(&divide, sizeof(int), 1, f);
    h[i] = ((float)num);
    //h[i] = ((float)num)/((float)divide);
    //scanf("%f",&h[i]);
}
fclose(f);

But this gives more than 6 decimal points.

Is there a way to get a float no. for eg. 34.123456000000000000000 i.e after 6th decimal place the rest of the value should be truncated.




Aucun commentaire:

Enregistrer un commentaire