This might be a simple question but I’m quite new to using Glitch so I’m not sure on this so bare with me.
I’m trying to randomize/shuffle 2 arrays, however, I want them to be shuffled the same way. I’ve done some research and it leads to using a seed. I don’t know how to use these though. I’ve tried importing Chance but that doesn’t work.
Thanks in advance,
I just looked this up and there’s a huge discussion on stackoverflow -
Also a few folks pointed to this (simple) answer but it’s controversial:
Hope that’s helpful! I haven’t specifically tried to do this myself.
supposing you have something like
const arrA = ['a1', 'a2', 'a3'];
const arrB = ['b1', 'b2', 'b3'];
const shuffledArrA = ?;
const shuffledArrB = ?;
two other ideas that don’t involve setting up a deterministic prng:
- instead of shuffling the two arrays, shuffle an array of numbers. then rearrange the two arrays based on the order of numbers in the shuffled array.
const indices = [0, 1, 2];
const shuffledIndices = shuffle(indices);
const shuffledArrA = indices.map((i) => arrA[i]);
const shuffledArrB = indices.map((i) => arrB[i]);
- build somewhat of a combined array where the elements are little arrays/objects each containing an item from each array, shuffle that, then separate them.
const arrAAndB = [['a1', 'b1'], ['a2', 'b2'], ['a3', 'b3']];
const shuffledArrAAndB = shuffle(arrAAndB);
const shuffledArrA = shuffledArrAAndB.map(([itemA, itemB]) => itemA);
const shuffledArrB = shuffledArrAAndB.map(([itemA, itemB]) => itemB);