-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadFile.f90
More file actions
83 lines (56 loc) · 1.71 KB
/
readFile.f90
File metadata and controls
83 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
MODULE word_dict
! Dict file should be an ordered list of words, all of a single length
! We shall default to 5 letters
IMPLICIT NONE
PRIVATE
INTEGER, PARAMETER :: wlen = 5
CHARACTER(LEN=12), PARAMETER :: dict_file="words_5.txt"
INTEGER :: dict_size
CHARACTER(LEN=wlen), DIMENSION(:), ALLOCATABLE :: dict
PUBLIC :: read_dict, get_random, in_dict
CONTAINS
SUBROUTINE read_dict()
INTEGER :: file_size, newline_sz, l_num, line
LOGICAL :: exists
INTEGER :: stat
INQUIRE(FILE=dict_file, EXIST=exists, SIZE=file_size)
newline_sz = LEN(NEW_LINE(" "))
IF(exists) THEN
dict_size = file_size/(wlen+newline_sz)
ALLOCATE(dict(dict_size))
OPEN(101, FILE=dict_file, ACTION='read', iostat=stat)
! This is not a very generic way to do this, but it is valid and simple
DO line = 1, dict_size
READ(101, '(A6)') dict(line)
END DO
END IF
END SUBROUTINE
FUNCTION get_random()
CHARACTER(LEN=wlen) :: get_random
INTEGER :: ind
ind = FLOOR(random() * dict_size)
get_random = dict(ind)
END FUNCTION
FUNCTION in_dict(word)
LOGICAL :: in_dict
CHARACTER(LEN=wlen), INTENT(IN) :: word
! Dumb find. Better alphabetic bisection should really be added
in_dict = ANY(dict == word)
END FUNCTION
FUNCTION random()
LOGICAL, SAVE :: init = .FALSE.
INTEGER :: sz, ct
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
REAL :: random
IF (.NOT. init) THEN
init = .TRUE.
CALL RANDOM_SEED(SIZE = sz)
ALLOCATE(seed(sz))
CALL SYSTEM_CLOCK(ct)
seed = ct
CALL RANDOM_SEED(PUT = seed)
DEALLOCATE(seed)
END IF
CALL RANDOM_NUMBER(random)
END FUNCTION random
END MODULE