Page 1 of 1

Random Numbers in C++

Posted: August 4th, 2003, 6:56 am
by JackHon
I am trying to write a function which draws a from a U(0,1) uniform distribution. I can use srand() and rand() to do this fairly sufficiently if I were to make just one sample.But as it is, I will be making repeated calls to the function for random numbers. I have planted the seed for srand() with the time() function, and hence there is an extremely high correlation between the random numbers, making it very unrandom as it is.Are there any algorithms out there, simple algorithms that I can use to generate this?Would it be better if i used a Linear Congruential Generators of some sort, and generated a list of pseudo-random numbers into an array and then pluck my samples from there?I am sure this is a much discussed topic in financial programming! Help Please!Thank yoU!

Random Numbers in C++

Posted: August 4th, 2003, 7:24 am
by Russell
You call srand once with the seed and then call rand repeatedly for the random numbers.However the standard library implementation should not really be relied upon, as the minimum standard for implementation of the function rand() is not very demanding statistically speaking.Instead try looking up Mersenne twister on Google. There is a C file that you can download that will give you a function for generating psuedo-random numbers with good statistical properties. Or if you want something not so extreme implement your own Linear Congruential Generator using 64 bit ints. Numerical recipes in C ch.6 (also on the web) should give you all the information you need.

Random Numbers in C++

Posted: August 4th, 2003, 7:40 am
by ametrano
QuantLib provides C++ implementation of the Mersenne Twister, along with other uniform random generators, low discrepancy generators (Sobol included, with the primitive polynomials modulo 2 and the direction numbers provided by Peter Jaeckel), inverse cumulative functions (Acklam and Moro algorythms), and more.All of this is free, cross-platform, and can be used with different C++ compilers. It is available in the current CVS. If you are not comfortable with CVS, you can use the release candidate at http://quantlib.org/gm/ or just wait for QuantLib 0.3.3 that will be released in a few weeks.hope it helpsciao -- Nando

Random Numbers in C++

Posted: August 4th, 2003, 10:42 pm
by JackHon
Thanks guys for your replies. I am using Microsoft Visual C++perhaps not the best choice really... and there are so much problems with name space and such that I can't use the Mersenne twister I found on google. Nvm.. thanks anyway.

Random Numbers in C++

Posted: August 5th, 2003, 5:48 am
by Russell
Jackhon, for "minimal" random number generation try this routine which makes use of 64 bit integer datatype, and seeds the randum number generator with an assembly language call to the readcpu function (this is Intel specific). You can replace the seed function with something else if you are not using an Intel processor.double __stdcall ran0(){ static const UINT64 a = 16807; static const UINT64 m = (static_cast<UINT64>(1) << 31) - 1; // this tells us whether we need to get a new seed or not: static bool Seeded = false; static UINT64 next; // Seed next with an unlikely value. if (Seeded == false) { // Read CPU timer and use this as our new 64 bit value: next = ReadCPUTimer(); Seeded = true; } // Algorithm is (a * next) mod(m) with a and m carefully chosen. next = (a * next) % m; return static_cast<double>(next) / m;}UINT64 ReadCPUTimer(){ volatile unsigned int ts_lo; volatile unsigned int ts_hi; // Read 64-bit CPU time stamp counter: // Result of rdtsc is lo 32-bits in eax, hi 32-bits in edx _asm { rdtsc mov dword ptr ts_lo, eax mov dword ptr ts_hi, edx }; return ((static_cast<INT64>(ts_hi))<<32) + ts_lo;}

Random Numbers in C++

Posted: August 5th, 2003, 2:30 pm
by tomtom
How fast does Mersenne Twister converse?I know Mersenne Twister is better from several aspect but not sure of the speed especialy for low dimention cases, say 5.

Random Numbers in C++

Posted: August 6th, 2003, 3:13 pm
by king
"rand() to do this fairly sufficiently if I were to make just one sample."you can generate a sequence of random numbers using rand(). consecutive calls to this function generates a sequence of random numbersnb you only need so seed rand once

Random Numbers in C++

Posted: August 7th, 2003, 3:44 pm
by luizvs
Hi Jack,Can you use a deterministic generator for U(0,1) or u need a volatile one? If yes, have you try the easy van der Corput for U(0,1)?

Random Numbers in C++

Posted: August 8th, 2003, 3:29 pm
by JackHon
I am not sure what you mean luizvus.A deterministic one as in one that changes according to just time?I don't need any underlying volatility factor to drive it like a brownian motion if thats what you mean.I just need it to be the distribution to exhibit a U(0,1) PDF if I draw infinite samples from it.Thanks

Random Numbers in C++

Posted: August 9th, 2003, 5:12 pm
by luizvs
Hi Jack,A deterministic generator is one that you always have the same sequence. So, if you are testing some algorithm or solution you can run it again and have the same sequence to compare results. It's very useful to make simulations that need to be comproved latter by others then you!! Even being deterministic, it's still random, but you have the rule for numbers generation!!!!Van der Corput is a well know way (and easy!!) to obtain a U(0,1). But, as I've mentioned, is deterministic (good for one, bad for others)!!

Random Numbers in C++

Posted: August 11th, 2003, 8:59 am
by mj
try boost www.boost.orgor Numerical recipes in CMJ