|
不知道你用的什么编译器?
如果是 gfortran 的话,你需要自己根据系统时间生成种子。Linux 下还可以访问 /dev/urandom 获取种子
[Fortran] 纯文本查看 复制代码 04 | subroutine random_arr ( a ) |
06 | integer ( 4 ) , intent ( out ) , allocatable :: a ( : ) |
09 | integer , allocatable :: A ( : ) |
10 | integer , allocatable :: B ( : ) |
12 | call init_random_seed ( ) |
20 | subroutine random_arr ( a ) |
24 | integer ( 4 ) , intent ( out ) , allocatable :: a ( : ) |
26 | call random_number ( temp ) |
33 | 30 call random_number ( temp 2 ) |
36 | if ( all ( a ( 1 : ( i -1 ) ) /= a ( i ) ) ) then |
45 | subroutine init_random_seed ( ) |
46 | use iso_fortran_env , only : int 64 |
48 | integer , allocatable :: seed ( : ) |
49 | integer :: i , n , un , istat , dt ( 8 ) , pid , lcg |
52 | call random_seed ( size = n ) |
56 | call date_and_time ( values = dt ) |
57 | t = ( dt ( 1 ) - 1970 ) * 365 _int 64 * 24 * 60 * 60 * 1000 & |
58 | + dt ( 2 ) * 31 _int 64 * 24 * 60 * 60 * 1000 & |
59 | + dt ( 3 ) * 24 _int 64 * 60 * 60 * 1000 & |
60 | + dt ( 5 ) * 60 * 60 * 1000 & |
61 | + dt ( 6 ) * 60 * 1000 + dt ( 7 ) * 1000 & |
65 | t = ieor ( t , int ( pid , kind ( t ) ) ) |
69 | call random_seed ( put = seed ) |
70 | end subroutine init_random_seed |
73 | use iso_fortran_env , only : int 64 |
79 | s = mod ( s , 4294967296 _int 64 ) |
81 | s = mod ( s * 279470273 _int 64 , 4294967291 _int 64 ) |
82 | lcg = int ( mod ( s , int ( huge ( 0 ) , int 64 ) ) , kind ( 0 ) ) |
|
|