RE: [Dev-C++] random numbers with double precision
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Mat...@Ve...> - 2005-02-16 22:25:11
|
Perhaps you should just get some raw hardware random numbers from some place like random.org or HotBits. Then just do something like this: void* p; while ((p+8)<=size) { unsigned long long* LL = reintepret_cast<unsigned long long*>(void* p); long double r = double(1/LL); p=p+8; } where p is a pointer to the random bits you loaded into ram somehow, maybe with fread() or some kind of simple stream code, whatever you use doesn't matter as long as it's binary mode. That should be a little bit tricky, but extremely accurate way to get some fractional numbers. You could also do the same general thing a little less accurately and randomly by making an array of shorts (up to 32767 is a short on i386) containing the output of many rand() calls. Then accumulate every so many shorts together into a long or long long with a pointer of some kind and your friend reinterpret_cast<T>(x) and divide 1 by this value to make some random fraction. Make sure you cast when you do the division just to make sure the compiler uses floating point math, so you'll get an accurate result. If you do this, don't define the array with values any bigger than rand() returns or you'll be adding predictable zeros to your random numbers, which is a bad thing to do. -----Original Message----- From: dev...@li... [mailto:dev...@li...]On Behalf Of Anton Zilman Sent: Wednesday, February 16, 2005 2:05 PM To: dev...@li... Subject: [Dev-C++] random numbers with double precision Hi, I need to produce random numbes uniformly distributed between 0 and 1. I can use (rand()%MAX_INT)/MAX_INT, but this gives me only 30 thousand something different numbers, because rand() is an integer. So, if I cannot simulate something probability of which is less than 1/MAX_INT. Is there an analog of drand() function in Dec C/C++? If anyone can help, it is apprecated! Anton ___________________________________________________________________ The information contained in this message and any attachment may be proprietary, confidential, and privileged or subject to the work product doctrine and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to this message and deleting it and all copies and backups thereof. Thank you. |