Scarlett
Scarlett

Reputation: 1

Netlogo: express probability to a turtle

I'm new to Netlogo and I'm now stuck with coding this sentence:

There should be a chance of 10% divided by the total number of turtles that a turtle will hatch a child.

The initial total number of turtles is 1.

So my code is:

let p (0.1 / 1)
    ask n-of (0.1 / 1) turtles 
    [hatch 1]

But it seems to me that my code may not be correct. Anyone any ideas how to change it?

I would appreciate any kinds of help. Many thanks in advance!

Upvotes: 0

Views: 136

Answers (2)

JenB
JenB

Reputation: 17678

How about this, put the random chance inside the ask

ask turtles
[ if random-float 1 < 0.1
  [ hatch 1
  ]
]

Part of the problem is that the question is badly expressed. "10% divided by the total number of turtles" doesn't really make sense, so I have interpreted it that approximately 10% of the turtles hatch a child turtle

Upvotes: 1

Tim Roberts
Tim Roberts

Reputation: 54698

"ask n-of" is clearly wrong, if there's only one turtle. And the random function only returns integers, so instead of checking for 0.1, let's choose a random number from 0 to 10 times the number of turtles. Something like this should work.

if random 10 < 1 [
  ask one-of turtles
    [hatch 1]
]

Here's my justification. Any given turtle has a 10%/N chance of hatching. That means that, 10% of the time, one random turtle has a 100% chance.

Say there are 5 turtles. By the spec, each turtle has a 2% chance of hatching (10%/5). 90% of the time, no one hatches. In the remaining 10% of the cases, 1 of the 5 will definitely get a chance. That means each individual turtle's chances are 10% x 20% which is 2%, as the spec says.

That's what the code does. "random 10" chooses a random number from 0 to 9. If that number is "< 1" (which means 0), then we choose a turtle at random to hatch.

Upvotes: 2

Related Questions