|
[Sponsors] |
Random generation is always same. How can I fix it? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
June 19, 2020, 16:47 |
Random generation is always same. How can I fix it?
|
#1 |
New Member
anonymous
Join Date: Mar 2019
Posts: 4
Rep Power: 7 |
Hi all
I am trying to make continuous random walk model for Lagrangian particle tracking. The problem I faced with is below. Whenever I try, number generated from random generation dose not change. Always same number came out. It seems that seed for random function is the problem but I am not familiar with C++ and all method I found turned out to be fail. Could you check my code and give me advice to fix it. I am using openFoam v7. xi is scalar variable I made for saving random number for each time step. PHP Code:
you can install my code by PHP Code:
|
|
June 20, 2020, 04:31 |
Check the seed
|
#2 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 11 |
I think it may be related with the seed provided. You always feed it with the same numbers so it returns the same numbers. I checked any OF code using Random and found that in brownianMotionForce a random source is included. In this case it uses the Random defined on the inherited class DSCMCloud as:
Code:
Random& rnd = this->owner().rndGen(); Anyway, if you keep pulling the thread you find that owner().rndGen() is calling a private method called rndGen_, defined inside the DSMCCloud. The code looks like this: Code:
// DSMCCloud.H ... Random rndGen_; ... // DSMCCloud.C ... rndGen_(label(971501) + 1526*Pstream::myProcNo()), ... Last edited by crubio.abujas; July 6, 2020 at 04:52. |
|
June 24, 2020, 11:41 |
|
#3 |
New Member
anonymous
Join Date: Mar 2019
Posts: 4
Rep Power: 7 |
Yes, it worked. Thanks Crubio.
For other's information. Main reason to my problem was that after Random generated, I loaded random number with specified seed from could object. |
|
July 6, 2020, 04:08 |
|
#4 |
Member
Join Date: Dec 2018
Posts: 75
Rep Power: 7 |
You have already solved your problem, but i share a perfect way of generating random numbers in C++ that i learned recently:
The solution is nearly perfect, the only issue left is running in parallel. This might seem a non-issue when we just want to implement random numbers for an application we only will use in serial. However, the trick is rather easy. We use the current time as seed value and add the PID. This will ensure, that when multiple processes are spawned at the same time, when starting a parallel run, each process has its unique seed value thanks to the contribution of the PID. Code:
1 // random stuff 2 # include " Random .H" 3 # include " clock .H" 4 Random ranGen ( clock :: getTime ()+pid ()); 5 6 for (int j = 0; j < 20; j ++) 7 { 8 Info << ranGen . integer (1, 100) << endl ; 9 } |
|
July 6, 2020, 05:04 |
|
#5 |
Senior Member
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 11 |
Hi hbulus,
I haven't check it, but I don't know if the parallel might be an issue with the approach I've suggested. I've been checking the Pstream::myProcNo() and it seems to return the process number (0, 1, 2, ...), so the seed shall be different for each of the processors. What I do found problematic is that the process number is not randomly distributed, so the seed are going to be the same for each processor (although different among them). The usage of clock and pid numbers you proposed seems to make a much more randomly seed. I find your approach more convenient so I'm wondering why the implementation of the cloud uses this fix seed approach. |
|
Tags |
random |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
divergence free random field | hnemati | Main CFD Forum | 3 | October 18, 2017 12:07 |
need reference for structured mesh generation | cfduser03 | Main CFD Forum | 3 | September 7, 2009 10:58 |
Natural Convection with heat generation | krishnachandranr | Main CFD Forum | 0 | July 28, 2009 05:22 |
Recognizing a random signal | cfd_newbie | FLUENT | 3 | January 10, 2008 18:26 |
3D Hyperbolic Grid Generation - Help Requested... | Marcus Lobbia | Main CFD Forum | 2 | November 9, 2003 06:53 |