CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Random number generation (https://www.cfd-online.com/Forums/openfoam/175482-random-number-generation.html)

fluidflowsteel July 31, 2016 09:09

Random number generation
 
Hi all,

I want to generate random numbers in OF 2.2.0 between -1 and +1 for the whole mesh initially which will remain same for all time steps. Can you advice how to do this ?


with regards,

Tobi August 12, 2016 10:29

Simple task,

there is a class called Random. An example is in my Gray-Scott-Solver ... https://bitbucket.org/shor-ty/graysc...e-view-default

jorkolino December 4, 2019 05:43

Quote:

Originally Posted by Tobi (Post 613803)
Simple task,

there is a class called Random. An example is in my Gray-Scott-Solver ... https://bitbucket.org/shor-ty/graysc...e-view-default

Do you know how to seed the random generator with arbitrary seed (for example CPU time clock) and not some fixed number, in your case '1'.

Tobi December 4, 2019 06:38

Hi,

sure, you just define a variable that changes and then you give it as an argument to the class.

Ramkumar21194 December 10, 2019 23:31

Hi, is it possible to generate random list of "N" numbers between given range (a,b)? using that class? Like i need some 10 numbers between say 0 to 100.


And it gives following error when i run that script.


Quote:

Test.C:13:40: error: ‘Foam::scalar Foam::Random::scalar01()’ is private within this context
const scalar randNumber(obj.scalar01());

vitor.geraldes@ist.utl.pt April 14, 2020 21:17

Example to generate a random number between 0 and 1
 
#include <iostream>
#include "fvCFD.H"
#include "Random.H"
#include "Time.H"

int main ()
{

Random randObj(clock::getTime());
Info << randObj.scalar01()<< endl;

}

Ramkumar21194 December 1, 2020 10:19

Quote:

Originally Posted by vitor.geraldes@ist.utl.pt (Post 765700)
#include <iostream>
#include "fvCFD.H"
#include "Random.H"
#include "Time.H"

int main ()
{

Random randObj(clock::getTime());
Info << randObj.scalar01()<< endl;

}


i am getting following error when i use your code.


Code:

testCode.C: In function ‘int main(int, char**)’:
testCode.C:50:29: error: ‘Foam::scalar Foam::Random::scalar01()’ is private within this context    Info << ranObj.scalar01();


olesen December 5, 2020 03:54

Quote:

Originally Posted by Ramkumar21194 (Post 789464)
i am getting following error when i use your code.


Code:

testCode.C: In function ‘int main(int, char**)’:
testCode.C:50:29: error: ‘Foam::scalar Foam::Random::scalar01()’ is private within this context    Info << ranObj.scalar01();


The scalar01() was in older code, try with the general method instead:
Code:

Random rng;
rng.sample01<scalar>();

Note that depending on your version of OpenFOAM, you can also use the generator op for the same thing, or with a different range. This can then be used as a generator function for C++ algorithms. For example,

Code:

Random::uniformGeneratorOp<scalar> randVal(0,10);
scalarField fld(1000);

std::generate (fld.begin(), fld.end(), randVal);

//  the same, as a loop

for (scalar& val : fld)
{
    val = randVal();
}

It even has a unary form that ignores its argument, which you can use to "swallow" the argument and return a random value instead. Replace something like magOp<scalar>() with your generator etc.


All times are GMT -4. The time now is 07:06.