LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Using /dev/random to generate double (http://www.linuxquestions.org/questions/programming-9/using-dev-random-to-generate-double-793610/)

shumi 03-06-2010 08:49 AM

Using /dev/random to generate double
 
I need to generate random numbers using /dev/random in C. The numbers should be of type double (64-bit floating point).
The functionality should be equal to linux command "od -An -N8 -t fD /dev/random", but written in C.

The prototype should be "double drand(void);".

Can someone plz tell me right way to do this?

H_TeXMeX_H 03-06-2010 09:14 AM

If you look in stdlib.h that is part of gcc, you'll see that there already exists a "drand48" function that does this.

shumi 03-06-2010 09:33 AM

description for drand48 says:
Quote:

Return non-negative, double-precision floating-point value in [0.0,1.0).
I need all values, both positive and negative, in range [DBL_MIN, DBL_MAX].
And I don't wont just to stretch the interval [0.0, 1.0) with multiplication.

Shell command "od -An -N8 -t fD /dev/random" does exactly what I want ...

Here is some code I wrote, but not sure if this is correct (IEEE related):
Code:

double drand(void) {
  double d;
  FILE* rand_fd = fopen("/dev/random", "r");
  fread(&d, 1, sizeof(double), rand_fd);
  fclose(rand_fd);
  return d;
}


pixellany 03-06-2010 09:41 AM

Quote:

Here is some code I wrote, but not sure if this is correct
Well.....does it work?

Quote:

And I don't wont just to stretch the interval [0.0, 1.0) with multiplication.
Why not?

shumi 03-06-2010 09:52 AM

Well the code seems to work ... it all looks nice :D , but just need some opinions if there should be handling of values such as infinities, negative zero or something else

I can't just stretch interval because I'm doing this for homework from numerical analysis ... stretching interval would make it have less possible values, then double type would lose it's efficiency (there was a discussion about this on class).

nadroj 03-06-2010 01:38 PM

Quote:

just need some opinions if there should be handling of values such as infinities, negative zero or something else
I'm not quite sure what you mean by this. Do you mean you want to "generate" +/- infinity, negative 0, etc? If using the above method, since it has range [0.0,1.0) you of course can't do that. I think I recall reading that "-0 == 0" evaluates to true, so you can ignore that "number" (also I think we generally don't ever consider "-0" as a number in analysis anyway). Also, +/- infinity aren't numbers, so you can't generate them as numbers. However, of course, you could use some flag or value, a, so that whenever a is generated, you "pretend" a +/- infinity has occurred.

Quote:

I can't just stretch interval because I'm doing this for homework from numerical analysis ... stretching interval would make it have less possible values
I think you should be fine if you scale the numbers. You could just multiple the number by a power of ten, 10, 100, 100, etc., depending on how large you want the integral part to be. Say the number 0.1234506789 is generated, you multiply it by 100 to get 12.3450678900... Alternatively, generate a random double to get a fractional number, and add a random integer to it.

Dan04 03-06-2010 02:58 PM

Well, what distribution do you want your values to have? You code will give all bit patterns as being equally likely. Is that really what you want?

shumi 03-06-2010 06:35 PM

Uniform distribution is preferable


All times are GMT -5. The time now is 01:55 PM.