Alter
Alter

Reputation: 3464

Netlogo - function should be random but is not

I have a simulation, and there have been cases where a function which should eventually go through all possible combinations, is not. So I started setting random-seed values until I could replicate it... sure enough, the function never returned some values that it eventually should have.

This is the function, is there something I'm using here which is causing the random part to break? - I just don't understand what is causing my code to fail and its frustrating me to no end.

to-report topk-newRandom [active-turtle k] ;returns a random list of turtles
  ;put the agent-set of turtles in a list of random order
  let topk sort n-of (count turtles with [document?]) turtles with [document?]

  ;remove turtles that are already connected to the active-turtle
  foreach topk [if (member? ? link-neighbors) [set topk remove ? topk]]

  ;limit the size of the list to being <= k
  if (length topk > k) [set topk sublist topk 0 k]
  report topk
end

Upvotes: 2

Views: 67

Answers (1)

Alter
Alter

Reputation: 3464

The problem was in that first line:
let topk sort n-of (count turtles with [document?]) turtles with [document?].
It was supposed to return a list of agents in random order, however it looks like it accomplished the exact opposite, the sort that was turning the agentset into a list also took out the random order ("sort" really should have been a huge red flag).

I looked up how to get a random ordered list of agents, and found this: [self] of turtles

Applying that to my code solved the problem. It also looks nicer

to-report topk-newRandom [active-turtle k]
  let topk [self] of turtles with [document?]
  foreach topk [if (member? ? link-neighbors) [set topk remove ? topk]]
  if (length topk > k) [set topk sublist topk 0 k]
  report topk
end

Upvotes: 2

Related Questions