Random numbers in C++

Post Reply
User avatar
tsn
Posts: 283
Joined: Fri Jun 11, 2010 7:22 am

Random numbers in C++

Post by tsn » Wed Feb 16, 2011 9:38 am

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.
Thomas Schlick | Next Limit Technologies

User avatar
Alex
Site Admin
Posts: 143
Joined: Mon Jun 14, 2010 9:35 am

Re: Random numbers in C++

Post by Alex » Wed Feb 16, 2011 9:48 am

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.
Alex Ribao
RealFlow Team
Next Limit Technologies

User avatar
tsn
Posts: 283
Joined: Fri Jun 11, 2010 7:22 am

Re: Random numbers in C++

Post by tsn » Wed Feb 16, 2011 10:03 am

Hi Alex,

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

Best,
Thomas
Thomas Schlick | Next Limit Technologies

Post Reply