This is a follow up question to NetLogo Efficient way to create fixed number of links. Having focussed on avoiding the nested `ask', I now have this code. It is much more efficient, but is creating too many links. Clearly a logic error but I can't see it.
globals
[ candidates
friends
]
to setup
clear-all
set friends 2
create-turtles 5000
set candidates turtles
make-network
end
to make-network
ask turtles
[ let new-links friends - count my-links
if new-links > 0
[ let chosen n-of min (list new-links count other candidates) other candidates
create-links-with chosen [ hide-link ]
set candidates other candidates
ask chosen [ if my-links = friends [ set candidates other candidates ] ]
]
]
end