| N = 100 | |
| # Plot a square of NxN small other squares. | |
| # Each one has a probability Pxi/N of being coloured, where i is the line where it stands. | |
| P = 0.5 | |
| im_size = 5 | |
| image = np.repeat(np.array(range(1,N+1)).reshape(N, 1), N, axis=1) | |
| # LESS understandable but executing FASTER | |
| image = (P/N * image) <= np.random.rand(N,N) | |
| # MORE understandable but executing SLOWER | |
| def bernoulli(num, P, N): | |
| return 1-np.random.binomial(1, P*num/N) | |
| vfunc = np.vectorize(bernoulli) | |
| image = vfunc(image, P, N) | |
| plt.figure(figsize=(im_size, im_size)) | |
| plt.imshow(image, cmap='gray') | |
| plt.show() |