Re: [fortran, doc] Improve random_seed example

2013-05-22 Thread Janne Blomqvist
On Tue, May 21, 2013 at 2:50 AM, Steve Kargl
 wrote:
> On Fri, May 17, 2013 at 03:18:01PM +0300, Janne Blomqvist wrote:
>> Hi,
>>
>> the example we provide for the usage of the random_seed intrinsic
>> could be better. At least one user has already been tripped over by
>> the fact that on some targets the first call to system_clock returns
>> 0, resulting in a poor seed. Below is an improved(?!) example program.
>>
>> Ok for trunk/4.8/4.7? (Changelog + patch is of course trivial once
>> there is agreement on the code itself)
>>
>
> Looks OK to me.
>
> It should probably also be noted in the manual that one can get
> real poor results if one does not set all seed values.

Thanks. Attached is the patch I committed following 'make pdf'
testing. Committed to trunk/4.8 (after Jakub's approval on IRC)/4.7.


--
Janne Blomqvist


rseed.diff
Description: Binary data


Re: [fortran, doc] Improve random_seed example

2013-05-20 Thread Steve Kargl
On Fri, May 17, 2013 at 03:18:01PM +0300, Janne Blomqvist wrote:
> Hi,
> 
> the example we provide for the usage of the random_seed intrinsic
> could be better. At least one user has already been tripped over by
> the fact that on some targets the first call to system_clock returns
> 0, resulting in a poor seed. Below is an improved(?!) example program.
> 
> Ok for trunk/4.8/4.7? (Changelog + patch is of course trivial once
> there is agreement on the code itself)
> 

Looks OK to me.

It should probably also be noted in the manual that one can get
real poor results if one does not set all seed values.

-- 
Steve


[fortran, doc] Improve random_seed example

2013-05-17 Thread Janne Blomqvist
Hi,

the example we provide for the usage of the random_seed intrinsic
could be better. At least one user has already been tripped over by
the fact that on some targets the first call to system_clock returns
0, resulting in a poor seed. Below is an improved(?!) example program.

Ok for trunk/4.8/4.7? (Changelog + patch is of course trivial once
there is agreement on the code itself)

! Seed the random generator with some random input
module rseed_rand
contains
  subroutine rseed()
implicit none
integer, allocatable :: seed(:)
integer :: i, n, un, istat, dt(8), pid, t(2), s
integer(8) :: count, tms

call random_seed(size = n)
allocate(seed(n))
! First try if the OS provides a random number generator
open(newunit=un, file="/dev/urandom", access="stream", &
 form="unformatted", iostat=istat)
if (istat == 0) then
   read(un) seed
   close(un)
else
   ! Fallback to XOR:ing the current time and pid. The PID is
   ! useful in case one launches multiple instances of the same
   ! program in parallel.
   call system_clock(count)
   if (count /= 0) then
  t = transfer(count, t)
   else
  call date_and_time(values=dt)
  tms = (dt(1) - 1970) * 365_8 * 24 * 60 * 60 * 1000 &
   + dt(2) * 31_8 * 24 * 60 * 60 * 1000 &
   + dt(3) * 24 * 60 * 60 * 60 * 1000 &
   + dt(5) * 60 * 60 * 1000 &
   + dt(6) * 60 * 1000 + dt(7) * 1000 &
   + dt(8)
  t = transfer(tms, t)
   end if
   s = ieor(t(1), t(2))
   pid = getpid()
   s = ieor(s, pid)
   if (n >= 3) then
  seed(1) = t(1)
  seed(2) = t(2)
  seed(3) = pid
  if (n > 3) then
 seed(4:) = s + 37 * (/ (i, i = 0, n - 4) /)
  end if
   else
  seed = s + 37 * (/ (i, i = 0, n - 1 ) /)
   end if
end if
call random_seed(put=seed)
  end subroutine rseed
end module rseed_rand

--
Janne Blomqvist