Skip to content

Instantly share code, notes, and snippets.

@dinhanhthi
Created October 30, 2021 19:11
Show Gist options
  • Save dinhanhthi/1802f242347c8546deddd568035c6221 to your computer and use it in GitHub Desktop.
Save dinhanhthi/1802f242347c8546deddd568035c6221 to your computer and use it in GitHub Desktop.
Plot a grid of squares
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment