ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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):
Well the code seems to work ... it all looks nice , 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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.