Might be partly due to plugging into different ports as somebody mentioned, but it does seem to happen randomly for me too. But I found this tool yesterday that makes it a lot simpler to solve https://github.com/jakehilborn/displayplacer - annoying that it doesn't work properly out of the box, of course