CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   About Random perturbation (https://www.cfd-online.com/Forums/openfoam-solving/59299-about-random-perturbation.html)

zou_mo July 13, 2005 10:58

I want to obtain a random valu
 
I want to obtain a random value at each 'imode' loop:
for(int imode=1;imode<=10;imode++)
{
Random pertA(1234567);
ampA=pertA.GaussNormal(); Info<<"ampA:"<<ampA<<endl;
}

But, it is strange that I got the same value for all 'imode' loop! Why?

By the way, some of the random values will be great than unit? Is something wrong?

henry July 13, 2005 11:05

Move Random pertA(1234567)
 
Move

Random pertA(1234567);

to before the loop.

zou_mo July 13, 2005 22:52

Thank you, Henry. The result i
 
Thank you, Henry. The result is nice. But some of the random values are big than 1 or less than -1. Why?

I cannot find the implementation file Random.C of the class Random. In the sub-directory 'src/OpenFOAM/primitives/random/', I found a file called Random.C which is not the right one. I use OpenFOAM1.1.

henry July 14, 2005 04:34

Random.C is the right one, Gau
 
Random.C is the right one, GaussNormal() is the last function in that file:

// return a normal Gaussian randon number
// with zero mean and unity variance N(0, 1)

scalar Random::GaussNormal()
{
static int iset = 0;
static scalar gset;
scalar fac, rsq, v1, v2;

if (iset == 0)
{
do
{
v1 = 2.0*scalar01() - 1.0;
v2 = 2.0*scalar01() - 1.0;
rsq = v1*v1 + v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);

fac = sqrt(-2.0 * log(rsq)/rsq);
gset = v1*fac;
iset = 1;

return v2*fac;
}
else
{
iset = 0;

return gset;
}
}


I am not sure if this method is required to limit the values between -1 and 1. If it is an issue for you I suggest you clip the values although this will probably distort the Gaussian distribution slightly.

henry July 14, 2005 05:08

That file is random.c not Rand
 
That file is random.c not Random.C, do you have a case-insensitive filing system?

zou_mo July 14, 2005 05:18

Sorry, I have found the right
 
Sorry, I have found the right Random.C file in my cluster machines where I have installed the full package. Maybe when I transfer the source files from the cluster machines to my local PC, the file random.C overrided Random.C.

Sorry for time wasting.

ziemowitzima May 18, 2006 05:22

Dear Zou Mo Gauss distributio
 
Dear Zou Mo
Gauss distribution with mean 0 and variance 1 , can give values less than -1 or higer than 1.

marhamat December 4, 2007 04:05

Hello everybody I want to o
 
Hello everybody

I want to obtain n random values for use at each runtime , by using OF Random-class.
But prooduced random values are similar in all runtime.

Is something wrong?
Regards
Marhamat

marhamat December 4, 2007 08:26

In Random points(1234567) wh
 
In Random points(1234567) what does mean (1234567)?And what does happen when i change 7 to 8? Why?

Thanks alot
Marhamat

gschaider December 4, 2007 09:06

I guess it is the seed of the
 
I guess it is the seed of the (pseudo)random sequence of numbers that is produced by the random-number-generator. By changing it you get a different sequence. But by fixing the seed you make sure that every simulation runs with the same sequence thus making the results reproducible.

marhamat December 5, 2007 09:15

Thanks alot Bernhard I want t
 
Thanks alot Bernhard
I want to use different sequence in different time steps;So i think i must change the number or arrangement of digits in Random points(1234567).
After testing some case,for example:
Random points(1234567);
Random points(1234568);
Random points(1434568);
Random points(1111);
Random points(3);
I get different sequance.
But i don't know the governing rules that change random sequence of numbers by changing the number or arrangement of digits.
Q1)What Does mean the number or arrangement of these digits?
Q2)How do i controll random-number-generator for producing different sequence in different time steps?
Is any references available in this field?
Please explan me more details.

With kind regards
Marhamat

gschaider December 5, 2007 13:59

A random number generator gene
 
A random number generator generates a "pseudo"-random number from the previous number. The number you give to the constructor is the start of the sequence (the predescessor of your first random number)

Have a look at http://en.wikipedia.org/wiki/Random_number_generator or "The Art of computer programming" Volume 2 by Donald E. Knuth (http://en.wikipedia.org/wiki/The_Art...er_Programming) if you REALLY want to know it in detail

anishtain4 January 30, 2013 16:59

I have a problem with this Random class, I pass the seed 0 which should make random numbers on each run but the numbers are getting repeated!!! How should I force it to generate different numbers at each run time?

gschaider January 31, 2013 17:30

Quote:

Originally Posted by anishtain4 (Post 405150)
I have a problem with this Random class, I pass the seed 0 which should make random numbers on each run but the numbers are getting repeated!!! How should I force it to generate different numbers at each run time?

Who says that a seed 0 should give different random sequences?

Just use different seeds if you want different results. The advantage of the behaviour you describe is that results are reproducible. Which is a good thing

anishtain4 February 3, 2013 07:49

Guess I read it somewhere that I can't remember it. By looking at Random.C I understand you are right, but what should I do if I want to have different seeds at every run automatically? I want to make a statistical study and changing the seed manually does not look so helpful.

gschaider February 3, 2013 09:45

Quote:

Originally Posted by anishtain4 (Post 405740)
Guess I read it somewhere that I can't remember it. By looking at Random.C I understand you are right, but what should I do if I want to have different seeds at every run automatically? I want to make a statistical study and changing the seed manually does not look so helpful.

The way non-scientific libraries do it when no seed is provided: the get the current time via the time()-function (see "man 3 time") and use it as a seed.

OR you read the seed from a dictionary. That dictionary is manipulated with a script that changes the seed ... using a pseudo-random value ... that has a fixed seed. That way you have a statistical study ... that is reproducible


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