CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

Random number generation

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 1 Post By Ramkumar21194
  • 3 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 31, 2016, 10:09
Default Random number generation
  #1
New Member
 
fluidflowsteel
Join Date: Jun 2016
Posts: 21
Rep Power: 10
fluidflowsteel is on a distinguished road
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,
fluidflowsteel is offline   Reply With Quote

Old   August 12, 2016, 11:29
Default
  #2
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
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
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   December 4, 2019, 06:43
Default
  #3
Member
 
George Pichurov
Join Date: Jul 2010
Posts: 52
Rep Power: 16
jorkolino is on a distinguished road
Quote:
Originally Posted by Tobi View Post
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'.
jorkolino is offline   Reply With Quote

Old   December 4, 2019, 07:38
Default
  #4
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi,

sure, you just define a variable that changes and then you give it as an argument to the class.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   December 11, 2019, 00:31
Default
  #5
New Member
 
Ramkumar
Join Date: Nov 2014
Location: pondicherry, India
Posts: 16
Rep Power: 12
Ramkumar21194 is on a distinguished road
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());
Ramkumar21194 is offline   Reply With Quote

Old   April 14, 2020, 22:17
Default Example to generate a random number between 0 and 1
  #6
New Member
 
Vitor Geraldes
Join Date: Dec 2009
Location: Lisbon, Portugal
Posts: 26
Rep Power: 17
vitor.geraldes@ist.utl.pt is on a distinguished road
#include <iostream>
#include "fvCFD.H"
#include "Random.H"
#include "Time.H"

int main ()
{

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

}
vitor.geraldes@ist.utl.pt is offline   Reply With Quote

Old   December 1, 2020, 11:19
Default
  #7
New Member
 
Ramkumar
Join Date: Nov 2014
Location: pondicherry, India
Posts: 16
Rep Power: 12
Ramkumar21194 is on a distinguished road
Quote:
Originally Posted by vitor.geraldes@ist.utl.pt View Post
#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();
Weitao likes this.
Ramkumar21194 is offline   Reply With Quote

Old   December 5, 2020, 04:54
Default
  #8
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Ramkumar21194 View Post
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.
olesen is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[snappyHexMesh] snappyHexMesh sticking point natty_king OpenFOAM Meshing & Mesh Conversion 11 February 20, 2024 10:12
[snappyHexMesh] Error snappyhexmesh - Multiple outside loops avinashjagdale OpenFOAM Meshing & Mesh Conversion 53 March 8, 2019 10:42
random number generation hwet Fluent UDF and Scheme Programming 9 February 29, 2016 20:35
MPI run run fails of quadcore openfoam2.4 Priya Somasundaran OpenFOAM Running, Solving & CFD 3 January 25, 2016 09:50
[blockMesh] --> foam fatal error: lillo763 OpenFOAM Meshing & Mesh Conversion 0 March 5, 2014 11:27


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