mojira.dev
MCPE-41092

Northwest Bias in Mob Movement

Update by @unknown

Steps to reproduce

  1. Make a reasonably large enclosure with a flat floor made of a single block type.

  2. Spawn mobs that have a wandering behavior at the center of the enclosure.

  3. Observe the mobs over a period of time.

Expected result

Mobs drift in all directions with equal probability. A group of mobs spreads out evenly in all directions.

Actual result

Mobs drift toward the north (-Z) and west (-X) over time. A group of mobs ends up bunched in the northwest corner of an enclosure.

Test world

See this comment.

Note: This bug existed in Java edition years ago and was fixed. See MC-10046 for details including code analysis. The issue seems to have been that using a floor function on a random number generated between maximum and minimum values results in a bias toward the minimum (e.g. the effective range is -10 to +9, instead of -10 to +10).


[media]


Original description
I've noticed that animal mobs tend to migrate to the northwest in my survival worlds. I've seen it in Bedrock on Windows 10 and iOS (iPhone).  It seems similar to an old Java version bug.

To test it, I created a flat world in my Bedrock Windows 10 version 1.8, and put a 2-block high barrier around an 11x11 chunk region.  I killed off everything, turned natural spawning off, then spawned 121 sheep in the center chunk.  After an hour and a half of waiting in the center chunk, I put fences around every chunk.  Code Connection helped with all this.

I then counted the sheep in each chuck. If the sheep spread out evenly, you would expect about one per chunk.  Instead, I got the counts shown in the attached image.  As you can see, there is a pretty strong bias towards the northwest.

Thanks.

Related issues

Attachments

Comments

migrated
[media][media][media][media][media]
[Mojang] Mega_Spud (Jay)

Cleaning up old tickets: This ticket has not been updated recently so is being closed as Cannot Reproduce. If you feel this is still a valid issue then please comment, or create a new ticket following the Issue Guidelines.

Also, we'd recommend making use of the search feature before writing a new bug report, as it's very likely that the issue has been mentioned already, and you can add a vote and any new information to the main ticket.

For any queries with your account or billing, you will need to contact Mojang Customer Support directly.

For feature requests or feedback, please head over to the Minecraft Feedback Site where you can search and vote for existing topics, or add a new thread if necessary.

Quick Links:
📓 Issue Guidelines – 💬 Mojang Support – 📧 Suggestions – 📖 Minecraft Wiki

GoldenHelmet

Reopened based on duplicate and related reports.

Confirmed in 1.14.60 using test world

[media]

This world tracks pigs and chickens wandering freely in a 120 x 120 fenced area. Synchronized repeating commanding blocks replace the blocks under chickens with yellow, and the blocks under pigs with pink, every 3 seconds. At 0 83 0 there is a lever that can be used to reset the ground to white and start a new trial. While the lever is "on," 1 pig and 1 chicken are summoned at the center of the fenced area once every 5 seconds. Hover at 0 75 0 and look straight down to view the entire test area. After running a trail for about 5 minutes, pigs and chickens begin to line up along the north and west fences, like this:

[media]
migrated

I ran the test again today and got a similar result. See the screen shot I uploaded. After about an hour, most sheep have migrated to the northwest corner of an 11 by 11 chunk area. Thanks for re-opening!

GoldenHelmet

This issue probably affects movement of all kinds of mobs, not just passive animals. It also may affect more than just wandering. Any behavior that involves picking a random block should be checked.

migrated

This also affects villagers and golems.  This may be part of whats causing issues with village population. Villagers and golems are wandering to the northwest corner of the village area.

GoldenHelmet

This bug was found to contribute to realm-breaking lag in REALMS-3089, as in conjunction with MCPE-45800 it can cause villagers to block each other from reaching their workstations, with the result that they continuously recalculate paths.

racartwright
[media]

Black squares track the movement of mobs over a several hours test.

I can confirm that thus bug is happening in 1.16 beta. I did some code-digging in 1.14.30 and I can see of two places where the code for finding a wandering position is biased in the NW direction. The location of the new wandering position is ultimately calculated by `RandomPos::generateRandomPos`. This is called by `RandomStrollGoal::_setWantedPosition` via a few intermediate functions. This function has a NW bias in two locations at least, and I don't think they are corrected for.

1) The game calls `RandomPos::_choosePositionOffset` to generate offsets from the mob's current position. In the X and Z directions, the game generates a random integer between 0 and 2D-1 (where D is a parameter), and the subtracts D from the integer. Therefore, in the X and Z directions the random offsets are uniformly distributed in the integers from -a to a-1. Clearly the average offset in the X and Z directions is -0.5.

2) The game calls `floor` on a mob's location before including the offset. If mobs are evenly distributed along a block, then this would also add a -0.5 offset in the X and Z directions.

Taken together, these biases over the long run would add -1 in the X and Z directions per run of `RandomStrollGoal::_setWantedPosition`, and produce the NW bias seen in the image.

The Fix

1) When drawing a random integer for the offset, use 2D+1 as the parameter.
2) Use a `round` instead of `floor` or add 0.5 to the current position before calling floor.

Alternative Fix

1) Keep the existing algorithm, but add 1 to calculated wander position in the X and Z direction to adjust for the biases.

R Code Demonstrating the bug and the fixes

# simulation size
n <- 1000000
# draw the location of a mob uniformly from -10 to 10
x0 <- runif(n,min=-10,max=10)

# Sample a wandering location the Bedrock way
d1 <- sample(seq.int(-10,9),n,replace=TRUE)
x1 <- floor(x0)+d1
print(mean(x1-x0)) # This will print something that is close to -1.

# Fixed algorithm
d2 <- sample(seq.int(-10,10),n,replace=TRUE)
x2 <- floor(x0+0.5)+d2
print(mean(x2-x0)) # This will print something that is close to 0

# Alternative fix
x3 <- x1+1
print(mean(x3-x0)) # This will print something that is close to 0

migrated

This issue is affecting my Bedrock Realm on 1.16.201

GoldenHelmet

Fixed in 1.17.0 or 1.17.10. It was not in a changelog but I have run the test world several times, including a 1-hour run, and the animals are staying evenly spread out.

UPDATE: still an issue for bees, see MCPE-143979.

migrated

(Unassigned)

368404

Confirmed

Multiple

Windows 10

wandering

1.16.220.52 Beta, 1.16.220.50 Beta, 1.16.210.58 Beta, 1.16.0.66 Beta, 1.14.1 Hotfix, 1.8.1, 1.14.0, 1.14.60 Hotfix, 1.16.201 Hotfix

1.17.10

Retrieved