Это даст вам 100 сеток 50х50 со случайными нулями и единицами с учетом вашего правила (равномерное (0, 1) <= .7) для заполнения <code>ones:
from random import uniform
import numpy as np
grids = np.array([[[0 if uniform(0, 1) <= .7 else 1 for i in range(50)] for j in range(50)] for _ in range(100)])
приведенный выше код заменяет приведенный ниже, а также дает 100 сеток за один проход:
import random
import numpy as np
x = [[random.uniform(0,1) for i in range(50)]for j in range(50)]
grid = np.array([[0 for i in range(len(x[0]))]for j in range(len(x))])
for i in range(len(x)):
for j in range(len(x[0])):
if x[i][j] <= 0.7:
grid[i][j] = 0
else:
grid[i][j] = 1
И вот подтверждение концепции:
>>> import numpy as np
>>> from random import uniform
>>> grids = np.array([[[0 if uniform(0, 1) <= .7 else 1 for i in range(50)] for j in range(50)] for _ in range(100)])
>>> grids
array([[[0, 1, 1, ..., 0, 1, 1],
[1, 1, 0, ..., 0, 0, 0],
[1, 1, 0, ..., 0, 1, 1],
...,
[1, 0, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
[0, 1, 0, ..., 0, 0, 1]],
[[0, 1, 1, ..., 0, 1, 0],
[1, 0, 0, ..., 0, 1, 1],
[1, 0, 0, ..., 0, 0, 0],
...,
[1, 1, 0, ..., 0, 0, 0],
[1, 1, 1, ..., 0, 1, 1],
[0, 0, 1, ..., 1, 1, 0]],
[[0, 1, 0, ..., 0, 1, 1],
[0, 0, 1, ..., 1, 0, 0],
[0, 0, 1, ..., 0, 0, 1],
...,
[0, 1, 1, ..., 1, 0, 0],
[0, 1, 0, ..., 0, 1, 1],
[0, 1, 0, ..., 0, 1, 0]],
...,
[[1, 0, 0, ..., 1, 1, 1],
[0, 1, 1, ..., 1, 0, 0],
[0, 0, 0, ..., 0, 1, 0],
...,
[0, 1, 1, ..., 0, 0, 1],
[0, 0, 1, ..., 0, 0, 0],
[1, 0, 1, ..., 0, 1, 0]],
[[1, 1, 1, ..., 1, 0, 1],
[0, 0, 1, ..., 1, 0, 1],
[0, 1, 1, ..., 1, 1, 1],
...,
[0, 0, 0, ..., 0, 0, 1],
[0, 0, 0, ..., 1, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[1, 1, 0, ..., 1, 1, 1],
[1, 1, 0, ..., 0, 0, 1],
[1, 1, 1, ..., 0, 1, 1],
...,
[1, 0, 1, ..., 1, 0, 1],
[1, 1, 0, ..., 0, 0, 0],
[1, 0, 1, ..., 0, 0, 1]]])
>>>
>>> len(grids)
100
>>> len(grids[0])
50
>>> len(grids[0][0])
50
>>>