Serving the Quantitative Finance Community

 
User avatar
JackHon
Topic Author
Posts: 0
Joined: July 21st, 2003, 5:37 pm

Random Numbers in C++

August 4th, 2003, 6:56 am

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!
 
User avatar
Russell
Posts: 1
Joined: October 16th, 2001, 5:18 pm

Random Numbers in C++

August 4th, 2003, 7:24 am

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.
 
User avatar
ametrano
Posts: 1
Joined: July 14th, 2002, 3:00 am

Random Numbers in C++

August 4th, 2003, 7:40 am

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
 
User avatar
JackHon
Topic Author
Posts: 0
Joined: July 21st, 2003, 5:37 pm

Random Numbers in C++

August 4th, 2003, 10:42 pm

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.
 
User avatar
Russell
Posts: 1
Joined: October 16th, 2001, 5:18 pm

Random Numbers in C++

August 5th, 2003, 5:48 am

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;}
 
User avatar
tomtom
Posts: 0
Joined: January 29th, 2003, 6:54 am

Random Numbers in C++

August 5th, 2003, 2:30 pm

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.
 
User avatar
king
Posts: 0
Joined: March 24th, 2002, 2:56 pm

Random Numbers in C++

August 6th, 2003, 3:13 pm

"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
 
User avatar
luizvs
Posts: 0
Joined: May 23rd, 2003, 6:13 pm

Random Numbers in C++

August 7th, 2003, 3:44 pm

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)?
 
User avatar
JackHon
Topic Author
Posts: 0
Joined: July 21st, 2003, 5:37 pm

Random Numbers in C++

August 8th, 2003, 3:29 pm

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
 
User avatar
luizvs
Posts: 0
Joined: May 23rd, 2003, 6:13 pm

Random Numbers in C++

August 9th, 2003, 5:12 pm

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)!!
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

Random Numbers in C++

August 11th, 2003, 8:59 am

try boost www.boost.orgor Numerical recipes in CMJ