Skip to content

Instantly share code, notes, and snippets.

@njvack
Last active October 2, 2020 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njvack/ebc754ea47dda6eb2e2c5ad6f7ac47d0 to your computer and use it in GitHub Desktop.
Save njvack/ebc754ea47dda6eb2e2c5ad6f7ac47d0 to your computer and use it in GitHub Desktop.
"""
Generates an array blocks * block_len long, containing 1 and 2.
It's shuffled such that there can never be more than block_len
consecutive repetitions of either value.
"""
import numpy as np
blocks = 100
block_len = 8
groupnums = (np.arange(blocks * block_len) % 2) + 1
blocked = groupnums.reshape(blocks, block_len)
np.apply_along_axis(np.random.shuffle, 1, blocked)
groups = blocked.reshape(blocks * block_len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment