Fix shared variable collisions in random number generation.

Reference #1223.