|
[Sponsors] |
July 30, 2009, 03:08 |
Globally instantiated "Random"
|
#1 |
Member
Andrew King
Join Date: Mar 2009
Location: Perth, Western Australia, Australia
Posts: 82
Rep Power: 17 |
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) Code:
//- Construct given seed Random(const label&); + Random(); // Member functions Code:
// construct given seed Random::Random(const label& seed) { + + static bool initialised = 0; + if (seed > 1) { Seed = seed; Code:
+ if (!initialised) { # ifdef USE_RANDOM srandom((unsigned int)Seed); # else srand48(Seed); # endif + initialised = 1; + } } +Random::Random() +{ + Random(1); +} + int Random::bit() 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 Code:
Random random(); scalar randno = random.scalar01(); Am i missing something? Cheers, Andrew
__________________
Dr Andrew King Fluid Dynamics Research Group Curtin University |
|
July 12, 2017, 11:22 |
|
#2 |
Member
Sebastian Trunk
Join Date: Mar 2015
Location: Erlangen, Germany
Posts: 60
Rep Power: 11 |
Hey Andrew,
your suggestion is exactly what I am looking for... Could you please offer the whole part from Random.C since I get an error Code:
my_test.C:52:28: error: request for member ‘scalar01’ in ‘random’, which is of non-class type ‘Foam::Random()’ scalar randno = random.scalar01(); ^ my_test.C:52:12: warning: unused variable ‘randno’ [-Wunused-variable] scalar randno = random.scalar01(); ^ Thanks Best wishes Sebastian |
|
|
|
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 21:50 |