code for generating random fields