dimanche 5 septembre 2021

Change randomly half of the values of list

I'm working on 2D list on python and try to hide some values from the grid:

Imagine this is the grid:

[[0, 0, 1, 1], 
[0, 1, 0, 1],
[1, 0, 1, 0],
[1, 1, 0, 0]]

the result I'm getting with this code:

    def startingArray(self,grid_copy): 
        for i in range (len(grid_copy)):
            n= self.CELL
            if n>len(grid_copy):
                grid_copy[i]=[2 for i in grid_copy]
            else:
                for i in range(n):
                    position=random.randrange(0,len(grid_copy[i]))
                    grid_copy[i][position]= 2  

[[2, 0, 1, 2],
 [2, 2, 2, 1],
 [2, 0, 2, 2],
 [2, 1, 2, 2]]

it's fine but, I want something where can hide exactly the half of the values randomly? Any ideas?




Aucun commentaire:

Enregistrer un commentaire