Adding Probability to the Game of Life

So I am working on adding probability to crate meshes that are either alive or dead. Using code from Game of Life I would like to establish rule sets that change the likelihood of a mesh coming back to life or dying.

Below is a piece of code where I would like to add an 80% chance to the ruleset but can’t manage to right the conditional.

 def applyGOL(self):
        #create temporary dictionary to save new values in
        newValues = {}
        #loop through x and y values
        for i in range(self.intX):
            for j in range(self.intY):
                #call function to find the sum of the neighbors
                sum = self.sumNeighbors(i, j)
                #run conditionals to change value 
                #INCLUDING PROBABILITY TO THE CODE
                #if a cell is alive(1)
                if self.valMTX[(i,j)] == 1:
                    #if the cell has less than 2 neighbors it has an 80% chance it dies(0)
                    if sum < 2: 
                        newValues[(i,j)] = 0
                    #if the cell has more than 3 neighbors it dies(0)
                    elif sum > 3:
                        newValues[(i,j)] = 0
                    #otherwise the cell stays alive(1)
                    else:
                        newValues[(i,j)] = 1
                #if the cell is dead(0) 
                else:
                    #it will come to life(1) if it has exactly 3 live neighbors
                    if sum == 3:
                        newValues[(i,j)] = 1

Sorry, I don’t know Python, but I think what you want to do is generate a random number (pseudo-random) within a range (say, 0 to 10) and then test whether that random number is greater than, in this case, 2. If the random number is greater than 2, the cell dies, otherwise not, because a random number within the range 0-10 has an ~80% chance of being greater than 2.

1 Like