Page 1 of 1

Random numbers in C++

Posted: Wed Feb 16, 2011 9:38 am
by tsn
We have to create random numbers between 0 and 1 in one of our projects. To create a statistical distribution between these numbers, the generator is called with each calculation cycle. The problem is that this distribution doesn't turn out very good, because the generator uses CPU ticks as a seed value and that's way too fast for our approach.

Maybe someone can point us to an alternative random number generator or another solution? Any idea is highly appreciated. Thanks a lot in advance.

Re: Random numbers in C++

Posted: Wed Feb 16, 2011 9:48 am
by Alex
Hi Thomas,

if you only set the seed once and call many times rand(), it should return some coherent random numbers.

However, if you are calling srand(seed) and then only using rand() one or two times, chances of having ugly numbers as a result are really high.

In fact, if I recall correctly, we had some problem with random numbers when using particle ids as seed. We needed to skip the first rand() returned value because it was unusable.

Re: Random numbers in C++

Posted: Wed Feb 16, 2011 10:03 am
by tsn
Hi Alex,

As always, thanks a lot for your super fast reply. We'll definitely give it a try.

Best,
Thomas