C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-16-2001, 01:58 PM   #1
Registered User
 
Join Date: Sep 2001
Posts: 412
Best way to generate a random double?

What's the best way to get a random double between 0 and 1?

I thought of rand() /RAND_MAX, but the problem with this is, it only returns rational numbers of the form x/32767. And 1/32767 is not all that small -- this "step size" is only on the order of 10^-5 -- so it's not really a good way to get a random double that should be random to more than this many decimal places. In other words, the smallest increment between random numbers generated by this method is on the order of 10^-5.

Currently, I have a function which uses a character array that is 20 characters long -- it randomly fills each with '0' through '9', it prepends "0." and uses the atof() to return the double corresponding to the string. This should give me a random number that is of the form x * 10^-20, so the smallest increment between random numbers is on the order of 10 ^ -20. This obviously gives a more uniform distribution over 0 and 1, and this is probably more than adequate for anything I'd need.

But, is there a better way to generate a random double than generate 20 random ints? I have to believe there's a more efficient way of getting a random double between 0 and 1 which is uniformly distributed to this degree.

My other idea was using rand to generate a random signed long like so:

long l;
l = rand() | ((long)rand() << 15) | (((long)rand() & 0x0001) << 30);

and then divide this by 0x7FFFFFFFL to get a random double.

(The shifting by 15 and 30, BTW, is because rand will return a value which is random in the first 15 bits).

The smallest increment here between random values is on the order of 10^-10, though, so the distribution isn't as uniform as the 20-char method.
The V. is offline   Reply With Quote
Old 10-16-2001, 02:12 PM   #2
Registered User
 
*pointer's Avatar
 
Join Date: Oct 2001
Posts: 74
Code:
double rand_float( double low, double high ) {
    return ( ( double )rand() * ( high - low ) ) / ( double )RAND_MAX + low;
}
__________________
pointer = NULL
*pointer is offline   Reply With Quote
Old 10-16-2001, 04:03 PM   #3
Registered User
 
Join Date: Sep 2001
Posts: 412
The problem with this, however, is that it is not uniformly distributed enough.

The reason is that there are only 32,768 different values of rand(). So, there will only be 32,768 different doubles that this can return -- it can return 0/32767, 1/32767, 2/32767, ... 32767/32767

The problem with this is that it is not uniform enough. It has equal probability of returning any multiple of 1/32767, but it has zero probability of returning anything *between* these values.

I'm looking for a way to generate a random double that is random over about 15 to 20 decimal places -- so there must be something in the range of 10^15 to 10^20 possible values that it can take on.
The V. is offline   Reply With Quote
Old 10-16-2001, 04:11 PM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
If you want better generators (better than the standard library function), then wander over to here....
http://directory.google.com/Top/Comp...andom_Numbers/
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help... darkconvoy C Programming 32 04-29-2008 03:33 PM
Certain functions Lurker C++ Programming 3 12-26-2003 01:26 AM
getline problem scottmanc C++ Programming 9 04-13-2003 09:27 PM
Generate random numbers in Lucky7 project using C# Grayson_Peddie C# Programming 1 04-11-2003 11:03 PM
Ask about how to generate random number which is not the same value at the same sec. ooosawaddee3 C++ Programming 11 07-16-2002 11:39 AM


All times are GMT -6. The time now is 11:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22