Skip to content

Instantly share code, notes, and snippets.

@prayerslayer
Last active September 29, 2017 20:43
Show Gist options
  • Save prayerslayer/249a30f591dfd0388d73486a37225931 to your computer and use it in GitHub Desktop.
Save prayerslayer/249a30f591dfd0388d73486a37225931 to your computer and use it in GitHub Desktop.
8x8 Matrix with 10 1s on random positions
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
return a;
}
function range(n) {
return new Array(n).fill(0).map((_, i) => i);
}
function chunk(arr, n = 8) {
return arr.reduce((arrs, x, i) => {
if (i % n === 0) {
arrs.push([])
}
const last = arrs[arrs.length - 1];
last.push(x)
return arrs;
}, [])
}
chunk(shuffle([...range(54).map(_ => 0), ...range(10).map(_ => 1)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment