[Fortran] 纯文本查看 复制代码
program main
implicit none
interface
subroutine random_arr(a)
implicit none
integer(4),intent(out),allocatable ::a(:)
end
end interface
integer , allocatable :: A(:)
integer , allocatable :: B(:)
integer len
call random_arr(A)
call random_arr(B)
print *,A
print *,B
end program
subroutine random_arr(a)
implicit none
real(4) temp,temp2
integer(4) siz,i
integer(4),intent(out),allocatable ::a(:)
call random_seed()
call random_number(temp)
print *,temp
siz=int(temp*20)
print *," ",siz," "
allocate (a(siz))
a(:)=0
loop:do i=1,siz
30 call random_number(temp2)
a(i) = int(temp2*20)
if(i/=1)then
if(all( a(1:(i-1))/=a(i)) )then
cycle loop
else
goto 30
end if
end if
End do loop
end subroutine
[Fortran] 纯文本查看 复制代码
program main
implicit none
interface
subroutine random_arr(a)
implicit none
integer(4),intent(out),allocatable ::a(:)
end
end interface
integer , allocatable :: A(:)
integer , allocatable :: B(:)
integer len
call init_random_seed()
call random_arr(A)
call random_arr(B)
print *,A
print *,B
end program
subroutine random_arr(a)
implicit none
real(4) temp,temp2
integer(4) siz,i
integer(4),intent(out),allocatable ::a(:)
call random_number(temp)
print *,temp
siz=int(temp*20)
print *," ",siz," "
allocate (a(siz))
a(:)=0
loop:do i=1,siz
30 call random_number(temp2)
a(i) = int(temp2*20)
if(i/=1)then
if(all( a(1:(i-1))/=a(i)) )then
cycle loop
else
goto 30
end if
end if
End do loop
end subroutine
subroutine init_random_seed()
use iso_fortran_env, only: int64
implicit none
integer, allocatable :: seed(:)
integer :: i, n, un, istat, dt(8), pid , lcg
integer(int64) :: t
call random_seed(size = n)
allocate(seed(n))
call system_clock(t)
if (t == 0) then
call date_and_time(values=dt)
t = (dt(1) - 1970) * 365_int64 * 24 * 60 * 60 * 1000 &
+ dt(2) * 31_int64 * 24 * 60 * 60 * 1000 &
+ dt(3) * 24_int64 * 60 * 60 * 1000 &
+ dt(5) * 60 * 60 * 1000 &
+ dt(6) * 60 * 1000 + dt(7) * 1000 &
+ dt(8)
end if
pid = getpid()
t = ieor(t, int(pid, kind(t)))
do i = 1, n
seed(i) = lcg(t)
end do
call random_seed(put=seed)
end subroutine init_random_seed
function lcg(s)
use iso_fortran_env, only: int64
integer :: lcg
integer(int64) :: s
if (s == 0) then
s = 104729
else
s = mod(s, 4294967296_int64)
end if
s = mod(s * 279470273_int64, 4294967291_int64)
lcg = int(mod(s, int(huge(0), int64)), kind(0))
end function lcg