From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31762 invoked by alias); 20 May 2016 20:12:44 -0000 Mailing-List: contact fortran-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: fortran-owner@gcc.gnu.org Received: (qmail 31743 invoked by uid 89); 20 May 2016 20:12:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 spammy=mixture, repeatable, states, learn X-HELO: mtaout004-public.msg.strl.va.charter.net Received: from mtaout004-public.msg.strl.va.charter.net (HELO mtaout004-public.msg.strl.va.charter.net) (68.114.190.29) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 20 May 2016 20:12:34 +0000 Received: from impout003 ([68.114.189.18]) by mtaout004.msg.strl.va.charter.net (InterMail vM.9.00.023.01 201-2473-194) with ESMTP id <20160520201232.PTLM22921.mtaout004.msg.strl.va.charter.net@impout003>; Fri, 20 May 2016 15:12:32 -0500 Received: from quattro.localdomain ([96.41.215.23]) by impout003 with charter.net id wkCX1s00B0Wrkg001kCXCM; Fri, 20 May 2016 15:12:32 -0500 X-Authority-Analysis: v=2.1 cv=S7tXwecP c=1 sm=1 tr=0 a=salB9WdMPIDduBH7JsZfrA==:117 a=salB9WdMPIDduBH7JsZfrA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=mDV3o1hIAAAA:8 a=GI5Q8yJknSLXjGFC2ogA:9 a=_umDqXBRtw2wuSt0:21 a=1ldGWSpuc1B2UwtW:21 a=QEXdDO2ut3YA:10 a=X2XqS4JWVTcA:10 a=9dvwdyh6gpcA:10 a=_FVE-zBwftR9WsbkzFJk:22 X-Auth-id: anZkZWxpc2xlQGNoYXJ0ZXIubmV0 Subject: Re: About the pseudo random generator in gfortran To: ch l , fortran@gcc.gnu.org References: From: Jerry DeLisle Message-ID: <3a05d4e2-3617-8fe2-d063-83a90678e51b@charter.net> Date: Fri, 20 May 2016 20:12:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2016-05/txt/msg00058.txt.bz2 On 05/20/2016 08:56 AM, ch l wrote: > Hi, > > I'm a ordinary user of The GNU Fortran Compiler. I'm still trying my best > to learn the Fortran programming language. When I am using The GNU Fortran > Compiler, I try to use the internal pseudo random generator like this: > > call random_seed() > call random_number(rnd) > > This code is quite common on many Fortran textbook or on many websites. The > 2nd line might also be use in a DO loop. Although I compile and run it for > many times, I get totally SAME random number(s). I have tested the same > code on Intel 's Fortran Compiler, Intel 's Fortran Compiler will give > different number for the every time I run it. > > I have tried this both on Windows (MinGW, TDM-GCC) and directly on Linux's > original GCC. They seem to have same problem. I also tried to search on > Google, but there seems no useful answer. > > I have read the official wiki, the example in the function RANDOM_NUMBER > (which URL is https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html > ) use a new SUBRUTINE named init_random_seed, and its define is in the > RANDOM_SEED page ( > https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED). > So I have tried the init_random_seed subroutine in my own code, it seems > works well, I get the different number for every times I run the program. > > But I'm really confused that why GNU's Fortran Compiler requires a external > subroutine to generate random number. So I wonder if this is a bug or it > is just designed to behave like this. It will be better if you can teach me > a lot. Maybe there is a more elegant way to generate random number. ;) > This is by design. For those people who want to have repeatable results they can use the same seed each time they run. Remember these are pseudo-random number generators. If you want truly unpredictable pseudo-random number sequences you must initialize the seed values in as random a way as possible. The example code in the manual does a pretty good job of this. If you are running on a linux based system with /dev/urandom, the linux kernal does a pretty good job of maintaining an "entropy pool" of a mixture of various CPU states, clocks, etc. The example code initializes the seeds using that, making the seed very unpredictable. Regards, Jerry