-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnametest.pas
68 lines (58 loc) · 1.39 KB
/
nametest.pas
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
program nametest;
uses gears,ghchars;
var
avg_name_length: LongInt;
Function NameAlreadyExists( LList: SAttPtr; name: String ): Boolean;
{ Return TRUE if name is found in LList, or FALSE otherwise. }
var
IsFound: Boolean;
begin
IsFound := False;
while ( LList <> Nil ) and ( not IsFound ) do begin
if LList^.Info = name then IsFound := True;
LList := LList^.Next;
end;
NameAlreadyExists := IsFound;
end;
Function NamesUntilRepeat: LongInt;
{ Start generating random names. Stop when a duplicate name is found. }
var
NList: SAttPtr;
N,L: LongInt;
name: String;
NameRepeated: Boolean;
begin
NList := nil;
N := 0;
L := 0;
NameRepeated := False;
repeat
name := UpCase( RandomName );
L := L + Length( name );
Inc( N );
if NameAlreadyExists( NList , name ) then begin
writeln( name , ' ' , N );
NameRepeated := True;
end else begin
StoreSAtt( NList , name );
end;
until ( N > 9999 ) or NameRepeated;
DisposeSAtt( NList );
if N > 0 then avg_name_length := avg_name_length + ( L div N );
NamesUntilRepeat := N;
end;
const
NumTrials = 5000;
var
T: Integer;
N: LongInt;
begin
Randomize;
N := 0;
avg_name_length := 0;
for T := 1 to NumTrials do begin
N := N + NamesUntilRepeat;
end;
writeln( 'Average: ' , N div NumTrials );
writeln( 'Average length: ' , avg_name_length div NumTrials );
end.