CFD Online Logo CFD Online URL
Home > Forums > OpenFOAM Programming & Development

Globally instantiated "Random"

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   July 30, 2009, 02:08
Default Globally instantiated "Random"
Andrew King
Join Date: Mar 2009
Location: Perth, Western Australia, Australia
Posts: 81
Rep Power: 8
andersking is on a distinguished road
Hi All,

I have a solver that I have I built that uses random numbers in a number of places. In the existing code, if a new "Random()" object is instantiated in more than one place, the random numbers are re-seeded (usually with the same seed) every time a new object is created.

In 1.5.x I changed Random() to initialise it self, and only reseed if explicitly asked. As follows:

(in Random.H)
         //- Construct given seed
         Random(const label&);
+       Random();
     // Member functions
(in Random.C)
  // construct given seed
 Random::Random(const label& seed)
+    static bool initialised = 0;
     if (seed > 1)
         Seed = seed;
+    if (!initialised) {
#       ifdef USE_RANDOM
            srandom((unsigned int)Seed);
#       else
#       endif
+        initialised = 1;
+    } 
+        Random(1);
 int Random::bit()
I am in the process of testing the solver in 1.6.x (git), and am about to port my changes to it.

But before I do, i need to know if what I am doing is a good idea?
will this have any unforseen side effects? (so far I haven't come across any).

Is it worth including upstream?

It should be entirely compatible with existing code, but allows you to grab a random number anywhere by using

Random random();
scalar randno = random.scalar01();
where ever you need it.

Am i missing something?

Dr Andrew King
Fluid Dynamics Research Group
Curtin University
andersking is offline   Reply With Quote


Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SimpleFOAM for steady globally unstable Reynolds number pbohorquez OpenFOAM Running, Solving & CFD 0 February 19, 2009 11:47
Problem with compile the setParabolicInlet ivanyao OpenFOAM Running, Solving & CFD 6 September 5, 2008 20:50

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