examples/life: Fix initial world randomization (#1045)

Because golang executes the init function after establishing
the variables in the var block, the random number seed wasn't
initializing until after the initial world state had gotten
established (leading to an identical game of Life on every run).
To fix this, we establish an empty world in the var block,
and then populate it in the init function after the random
number generator has been seeded.
This commit is contained in:
dericmiller 2020-01-05 23:09:51 -05:00 committed by Hajime Hoshi
parent 9be3495077
commit 7ee8d1aa5f

View File

@ -53,6 +53,7 @@ func NewWorld(width, height int, maxInitLiveCells int) *World {
func init() {
rand.Seed(time.Now().UnixNano())
world = NewWorld(screenWidth, screenHeight, int((screenWidth*screenHeight)/10))
}
// init inits world with a random state.
@ -156,7 +157,7 @@ const (
)
var (
world = NewWorld(screenWidth, screenHeight, int((screenWidth*screenHeight)/10))
world *World
pixels = make([]byte, screenWidth*screenHeight*4)
)