LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2010, 08:49 AM   #1
shumi
LQ Newbie
 
Registered: Sep 2006
Location: Croatia
Distribution: FC17
Posts: 10

Rep: Reputation: 0
Angry 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?
 
Old 03-06-2010, 09:14 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
If you look in stdlib.h that is part of gcc, you'll see that there already exists a "drand48" function that does this.
 
Old 03-06-2010, 09:33 AM   #3
shumi
LQ Newbie
 
Registered: Sep 2006
Location: Croatia
Distribution: FC17
Posts: 10

Original Poster
Rep: Reputation: 0
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;
}
 
Old 03-06-2010, 09:41 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 03-06-2010, 09:52 AM   #5
shumi
LQ Newbie
 
Registered: Sep 2006
Location: Croatia
Distribution: FC17
Posts: 10

Original Poster
Rep: Reputation: 0
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).
 
Old 03-06-2010, 01:38 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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.

Last edited by nadroj; 03-06-2010 at 01:41 PM.
 
Old 03-06-2010, 02:58 PM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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?
 
Old 03-06-2010, 06:35 PM   #8
shumi
LQ Newbie
 
Registered: Sep 2006
Location: Croatia
Distribution: FC17
Posts: 10

Original Poster
Rep: Reputation: 0
Uniform distribution is preferable
 
  


Reply

Tags
double, programming, random



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I need to generate random number either 1 or 0? mvmacd Linux - General 4 01-21-2010 06:50 PM
Generate random numbers in C program ssaslam Linux - Newbie 5 10-23-2008 08:28 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration