CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   problem with rand() (https://www.cfd-online.com/Forums/main/146695-problem-rand.html)

alisina-s January 4, 2015 14:06

problem with rand()
 
Hi Everybody
I am using rand function in my solver and somehow I need random numbers between 0 and 6 but the problem is that when I simply use rand() function, I get unexpected huge numbers and these numbers are constant run by run.
I have read somewhere in forum that rand(an integer) should work.but in my case I have error.
can anyone help me with this?
thanks in advance

mprinkey January 4, 2015 14:36

rand() returns an int, so you need to normalize by RAND_MAX and convert to float/double if necessary. And if you don't use srand() to seed the PRNG, you will always get the same sequence.

http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

alisina-s January 7, 2015 10:08

thanks Micheal
now it is working fine


All times are GMT -4. The time now is 08:12.