I don't think this is anything to do with memory sharing. You're just seeing the behaviour of rand
.
With that line commented out, each child calls rand
for the first time, so each gets seeded independently.
With that line uncommented, then rand
is seeded before any of the children are forked. So they all see the same seed.