Serving the Quantitative Finance Community

 
User avatar
yaourt
Topic Author
Posts: 0
Joined: January 21st, 2009, 12:50 pm

C++: same value when generating random variable

May 17th, 2010, 8:38 am

I want to write a function to generate a uniformly distributed random variable and a standard normally distributed random variable (see below c++ code). when i call the unirand function, I always get the same value. Why is that?Thanks!#include <iostream>#include <cstdlib>#include <ctime> #include <cmath> using namespace std; double unirand(int number =1){ srand((unsigned)time(0)); double random; random = (rand()%100+1); random = random /100; return random; };double standardnormal(){ //Box-Muller-transform// double z0, z1; const double pi = 3.14; double u1,u2; u1=unirand(); u2=unirand(); cout << u1 << " = "<<u2<< " <- u1 and u2 have the same value. Why?"<< endl; z0 = sqrt(-2*log(u1))*cos(2*pi*u2); //z1 = sqrt(-2*log(u1))*sin(2*pi*u2); return z0; };int main(){ standardnormal(); system("pause"); };
 
User avatar
Hansi
Posts: 41
Joined: January 25th, 2010, 11:47 am

C++: same value when generating random variable

May 17th, 2010, 8:56 am

Because you are setting the random number seed to time(0) every time the function is called:http://www.cplusplus.com/reference/clib ... lib/srand/
 
User avatar
untwigged
Posts: 2
Joined: January 14th, 2006, 3:21 pm

C++: same value when generating random variable

May 17th, 2010, 10:23 am

Maybe it's random...
 
User avatar
yaourt
Topic Author
Posts: 0
Joined: January 21st, 2009, 12:50 pm

C++: same value when generating random variable

May 17th, 2010, 10:34 am

QuoteOriginally posted by: HansiBecause you are setting the random number seed to time(0) every time the function is called:http://www.cplusplus.com/reference/clib ... nd/alright, i need to execute "srand((unsigned)time(0));" only at the begining before generating the random numbers (e.g. including it in the int main() ) . although, i dont quite understand why this is the case. if you use srand((unsigned)time(0)); later in the program the system time is differnt and, hence, the seed should be different from when it was first called. thanks anyways
 
User avatar
Hansi
Posts: 41
Joined: January 25th, 2010, 11:47 am

C++: same value when generating random variable

May 17th, 2010, 10:52 am

It's basically related to not enough time having passed, e.g. add #include <windows.h> and set it to:Sleep(1000);srand((unsigned)time(NULL));But it's best practice to just set the seed once for simple things like this unless you need to draw the same numbers again for some reason.If you want to use a more robust RNG then the default rand() you might want to check out the Boost RNG:http://www.boost.org/doc/libs/1_43_0/do ... andom.html
 
User avatar
Hansi
Posts: 41
Joined: January 25th, 2010, 11:47 am

C++: same value when generating random variable

May 17th, 2010, 10:57 am

Use #include <pthread.h> is on a posix system.
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

C++: same value when generating random variable

May 17th, 2010, 11:01 am

QuoteOriginally posted by: HansiIt's basically related to not enough time having passed, e.g. add #include <windows.h> and set it to:Sleep(1000);srand((unsigned)time(NULL));But it's best practice to just set the seed once for simple things like this unless you need to draw the same numbers again for some reason.If you want to use a more robust RNG then the default rand() you might want to check out the Boost RNG:http://www.boost.org/doc/libs/1_43_0/do ... om.htmlYou guys seem to be getting on like a house on fire, but if I could just inform of my blog on Boost.Random There's a ready-to-run example. Mersenne Twister.rand() is scary..
Last edited by Cuchulainn on May 16th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++: same value when generating random variable

May 17th, 2010, 11:22 am

QuoteOriginally posted by: CuchulainnIf you want to use a more robust RNG then the default rand() you might want to check out the Boost RNG:http://www.boost.org/doc/libs/1_43_0/do ... om.htmlwow, this Boost example makes Java look terse. i didn't know you can make C++ so verbose
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

C++: same value when generating random variable

May 17th, 2010, 11:26 am

QuoteOriginally posted by: jawabeanQuoteOriginally posted by: CuchulainnIf you want to use a more robust RNG then the default rand() you might want to check out the Boost RNG:http://www.boost.org/doc/libs/1_43_0/do ... om.htmlwow, this Boost example makes Java look terse. i didn't know you can make C++ so verboseAn OO option is to create two class hierachies using polymorphic functions and inheritance. Performance is 10 times slower. But another way is CRTP pattern if you insist. BTW that was Hansi's post. BTW SHOW US your Java RNG
Last edited by Cuchulainn on May 16th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++: same value when generating random variable

May 17th, 2010, 11:36 am

QuoteOriginally posted by: Cuchulainn BTW SHOW US your Java RNGstandard one goes like this:double r = Math.random(); if you want more control then:Random rng = new Random(seed);r = rng.nextDouble();r = rng.nextGaussian();If you don't like a standard one, there are many options, e.g. with apache.commons.math:RandomGenerator rng = new MersenneTwister();rng.setSeed(seed);r = rng.nextDouble();
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

C++: same value when generating random variable

May 17th, 2010, 12:01 pm

QuoteOriginally posted by: jawabeanQuoteOriginally posted by: Cuchulainn BTW SHOW US your Java RNGstandard one goes like this:double r = Math.random(); if you want more control then:Random rng = new Random(seed);r = rng.nextDouble();r = rng.nextGaussian();If you don't like a standard one, there are many options, e.g. with apache.commons.math:RandomGenerator rng = new MersenneTwister();rng.setSeed(seed);r = rng.nextDouble();Looks nice.
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++: same value when generating random variable

May 17th, 2010, 12:08 pm

QuoteOriginally posted by: CuchulainnLooks nice.Java standard Random is very slow. i use it only for prototyping. Java interfaces are usually clean and simple, but in this case i'm missing getSeed(). it would be nice to be able to get a seed in the middle of the sequence, so to use it later to reproduce the rest of the sequence.
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

C++: same value when generating random variable

May 17th, 2010, 12:22 pm

QuoteOriginally posted by: jawabeanQuoteOriginally posted by: CuchulainnLooks nice.Java standard Random is very slow. i use it only for prototyping. Java interfaces are usually clean and simple, but in this case i'm missing getSeed(). it would be nice to be able to get a seed in the middle of the sequence, so to use it later to reproduce the rest of the sequence.What's the reason? the polympoprhic behaviour or floats. or both?
 
User avatar
quantmeh
Posts: 0
Joined: April 6th, 2007, 1:39 pm

C++: same value when generating random variable

May 17th, 2010, 12:53 pm

QuoteOriginally posted by: CuchulainnWhat's the reason? the polympoprhic behaviour or floats. or both?i think it's because they try to make it very safe. they use a class called AtomicLong, which calls a native method. all it does is updates the value of the seed. i couldn't rationalize this implementation though