How does this work in a real lottery situation? You have 39 balls, each with a different number on, and you pick seven out, one by one. When you remove a ball, it can't be picked again because it doesn't exist any more.
You problem is here:
for k:=1 to 7 do b[k]:=random(39)+1;
How about this (I don't know if this is proper Pascal as I haven't used it in like 12 years)
for k:=1 to 7 do begin
repeat
b[k]:=random(39)+1;
p:=0;
for j:=1 to k do if b[k]=b[j] then p:=1;
until p=0;
end;
This has a risk of infinite run time if the random function returns the same number infinitely, so you'd ideally code around that somehow.
This wouldn't work, I think mostly because you never defined b[j], the program compares b[k] to nothing.
The problem is that same numbers can appear but you can't really make them non-exsit anymore you would have to ask yourslef if the numbers are same but it would be like that; If the first number generated is the same as the second then the second number is the first+1 but then you would have to ask yourself for the third number if it's same as the first and the second and so on, this would get really long and the numbers would likely be always +1, e.g. 10,11,12,13,14...