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

 July 13, 2005, 10:58 I want to obtain a random valu #1 Member   zoujianfeng Join Date: Mar 2009 Location: Hangzhou, China Posts: 30 Rep Power: 14 I want to obtain a random value at each 'imode' loop: for(int imode=1;imode<=10;imode++) { Random pertA(1234567); ampA=pertA.GaussNormal(); Info<<"ampA:"<

 July 13, 2005, 11:05 Move Random pertA(1234567) #2 Senior Member   Join Date: Mar 2009 Posts: 854 Rep Power: 19 Move Random pertA(1234567); to before the loop.

 July 13, 2005, 22:52 Thank you, Henry. The result i #3 Member   zoujianfeng Join Date: Mar 2009 Location: Hangzhou, China Posts: 30 Rep Power: 14 Thank you, Henry. The result is nice. But some of the random values are big than 1 or less than -1. Why? I cannot find the implementation file Random.C of the class Random. In the sub-directory 'src/OpenFOAM/primitives/random/', I found a file called Random.C which is not the right one. I use OpenFOAM1.1.

 July 14, 2005, 04:34 Random.C is the right one, Gau #4 Senior Member   Join Date: Mar 2009 Posts: 854 Rep Power: 19 Random.C is the right one, GaussNormal() is the last function in that file: // return a normal Gaussian randon number // with zero mean and unity variance N(0, 1) scalar Random::GaussNormal() { static int iset = 0; static scalar gset; scalar fac, rsq, v1, v2; if (iset == 0) { do { v1 = 2.0*scalar01() - 1.0; v2 = 2.0*scalar01() - 1.0; rsq = v1*v1 + v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0 * log(rsq)/rsq); gset = v1*fac; iset = 1; return v2*fac; } else { iset = 0; return gset; } } I am not sure if this method is required to limit the values between -1 and 1. If it is an issue for you I suggest you clip the values although this will probably distort the Gaussian distribution slightly.

 July 14, 2005, 05:08 That file is random.c not Rand #5 Senior Member   Join Date: Mar 2009 Posts: 854 Rep Power: 19 That file is random.c not Random.C, do you have a case-insensitive filing system?

 July 14, 2005, 05:18 Sorry, I have found the right #6 Member   zoujianfeng Join Date: Mar 2009 Location: Hangzhou, China Posts: 30 Rep Power: 14 Sorry, I have found the right Random.C file in my cluster machines where I have installed the full package. Maybe when I transfer the source files from the cluster machines to my local PC, the file random.C overrided Random.C. Sorry for time wasting.

 May 18, 2006, 05:22 Dear Zou Mo Gauss distributio #7 Senior Member   Mieszko Młody Join Date: Mar 2009 Location: POLAND, USA Posts: 144 Rep Power: 14 Dear Zou Mo Gauss distribution with mean 0 and variance 1 , can give values less than -1 or higer than 1.

 December 4, 2007, 03:05 Hello everybody I want to o #8 Senior Member   Marhamat Zeinali Join Date: Mar 2009 Location: Tehran, Tehran, iran Posts: 107 Rep Power: 14 Hello everybody I want to obtain n random values for use at each runtime , by using OF Random-class. But prooduced random values are similar in all runtime. Is something wrong? Regards Marhamat

 December 4, 2007, 07:26 In Random points(1234567) wh #9 Senior Member   Marhamat Zeinali Join Date: Mar 2009 Location: Tehran, Tehran, iran Posts: 107 Rep Power: 14 In Random points(1234567) what does mean (1234567)?And what does happen when i change 7 to 8? Why? Thanks alot Marhamat

 December 4, 2007, 08:06 I guess it is the seed of the #10 Assistant Moderator   Bernhard Gschaider Join Date: Mar 2009 Posts: 4,209 Rep Power: 48 I guess it is the seed of the (pseudo)random sequence of numbers that is produced by the random-number-generator. By changing it you get a different sequence. But by fixing the seed you make sure that every simulation runs with the same sequence thus making the results reproducible. __________________ Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 December 5, 2007, 08:15 Thanks alot Bernhard I want t #11 Senior Member   Marhamat Zeinali Join Date: Mar 2009 Location: Tehran, Tehran, iran Posts: 107 Rep Power: 14 Thanks alot Bernhard I want to use different sequence in different time steps;So i think i must change the number or arrangement of digits in Random points(1234567). After testing some case,for example: Random points(1234567); Random points(1234568); Random points(1434568); Random points(1111); Random points(3); I get different sequance. But i don't know the governing rules that change random sequence of numbers by changing the number or arrangement of digits. Q1)What Does mean the number or arrangement of these digits? Q2)How do i controll random-number-generator for producing different sequence in different time steps? Is any references available in this field? Please explan me more details. With kind regards Marhamat

 December 5, 2007, 12:59 A random number generator gene #12 Assistant Moderator   Bernhard Gschaider Join Date: Mar 2009 Posts: 4,209 Rep Power: 48 A random number generator generates a "pseudo"-random number from the previous number. The number you give to the constructor is the start of the sequence (the predescessor of your first random number) Have a look at http://en.wikipedia.org/wiki/Random_number_generator or "The Art of computer programming" Volume 2 by Donald E. Knuth (http://en.wikipedia.org/wiki/The_Art...er_Programming) if you REALLY want to know it in detail __________________ Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 January 30, 2013, 15:59 #13 Senior Member   Mahdi Hosseinali Join Date: Apr 2009 Location: NB, Canada Posts: 272 Rep Power: 15 I have a problem with this Random class, I pass the seed 0 which should make random numbers on each run but the numbers are getting repeated!!! How should I force it to generate different numbers at each run time?

January 31, 2013, 16:30
#14
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,209
Rep Power: 48
Quote:
 Originally Posted by anishtain4 I have a problem with this Random class, I pass the seed 0 which should make random numbers on each run but the numbers are getting repeated!!! How should I force it to generate different numbers at each run time?
Who says that a seed 0 should give different random sequences?

Just use different seeds if you want different results. The advantage of the behaviour you describe is that results are reproducible. Which is a good thing
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

 February 3, 2013, 06:49 #15 Senior Member   Mahdi Hosseinali Join Date: Apr 2009 Location: NB, Canada Posts: 272 Rep Power: 15 Guess I read it somewhere that I can't remember it. By looking at Random.C I understand you are right, but what should I do if I want to have different seeds at every run automatically? I want to make a statistical study and changing the seed manually does not look so helpful.

February 3, 2013, 08:45
#16
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,209
Rep Power: 48
Quote:
 Originally Posted by anishtain4 Guess I read it somewhere that I can't remember it. By looking at Random.C I understand you are right, but what should I do if I want to have different seeds at every run automatically? I want to make a statistical study and changing the seed manually does not look so helpful.
The way non-scientific libraries do it when no seed is provided: the get the current time via the time()-function (see "man 3 time") and use it as a seed.

OR you read the seed from a dictionary. That dictionary is manipulated with a script that changes the seed ... using a pseudo-random value ... that has a fixed seed. That way you have a statistical study ... that is reproducible
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

March 5, 2018, 02:11
#17
New Member

liyuan
Join Date: Jul 2015
Posts: 8
Rep Power: 8
Quote:
 Originally Posted by gschaider The way non-scientific libraries do it when no seed is provided: the get the current time via the time()-function (see "man 3 time") and use it as a seed. OR you read the seed from a dictionary. That dictionary is manipulated with a script that changes the seed ... using a pseudo-random value ... that has a fixed seed. That way you have a statistical study ... that is reproducible
what does “see "man 3 time"” mean above？

March 5, 2018, 03:06
#18
Assistant Moderator

Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,209
Rep Power: 48
Quote:
 Originally Posted by yuanlee2011 what does “see "man 3 time"” mean above？
The command to get the relevant manual page on the command line
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request

March 5, 2018, 03:25
#19
New Member

liyuan
Join Date: Jul 2015
Posts: 8
Rep Power: 8
Quote:
 Originally Posted by gschaider The command to get the relevant manual page on the command line

 September 5, 2018, 09:02 #20 Senior Member   Guilherme Join Date: Apr 2017 Posts: 181 Rep Power: 6 Is it possible to implement the choice of values -1 or 1 in the code? I'm trying to induce a random number at velocity, but I also want to make it toggle (adding and subtracting to the original value)...