/** * Choosing what `r` plays. * * The obvious implementation — roll, and if it lands on the current track roll * again — is a loop whose length you cannot bound, and the second obvious one, * `if (at === avoid) at++`, is a small statistical lie: the track after the * current one becomes twice as likely as any other, because it is both its own * outcome and the collision outcome. Pressing `r` a few hundred times on one * album would show it. * * So the roll is taken over `count - 1` outcomes and mapped around the excluded * index, which is uniform by construction: `0..count-2` is a bijection onto * "every index except that one". * * `random` is a parameter rather than `Math.random()` at the call site for the * usual reason — the tests need to pin it — and the guard on its output is * there because a broken rng should land on a valid track, not throw from a * keypress. */ /** * A uniform index in `0 … count-1`, excluding `avoid` when it is one of them. * Returns -1 when there is nothing to choose (an empty list). */ export function randomIndex(count: number, avoid = -1, random: () => number = Math.random): number { if (!Number.isInteger(count) || count <= 0) return -1; const excluded = avoid >= 0 && avoid < count ? avoid : -1; // One track: there is nothing else, and "play something else" loses to "play // the only thing there is". It also keeps the span below from going to zero. if (count === 1) return 0; const span = excluded >= 0 ? count - 1 : count; const roll = random(); if (!(roll >= 0) || !(roll < 1)) { // NaN, a negative, or an rng that returns exactly 1 — still pick something // legal rather than hand the keypress back as an exception. return excluded >= 0 ? (excluded + 1) % count : 0; } let at = Math.floor(roll * span); if (excluded >= 0 && at >= excluded) at++; return at; } /** * Where `r` drops you into a track (shift+r plays it straight). * * A uniformly random position is a bad random position: most of its outcomes * are either the first ten seconds (indistinguishable from not pressing the * key at all) or the last ten (a track that announces itself by ending). So * the draw is taken over the part of the record where being dropped in is the * point, and a short track gets no random start at all — on a 40-second piece, * jumping in is just skipping most of it. */ export const RANDOM_START = { /** Below this the track is played from the top: there is no middle to find. */ shortestTrack: 45, /** Nothing before this counts as a drop-in; it would look like doing nothing. */ earliest: 10, /** Never leave less than this: a random start that ends the track is a bug impression. */ tail: 20, } as const; export function randomStart( duration: number | undefined, random: () => number = Math.random, ): number { const d = Number.isFinite(duration) ? (duration as number) : 0; const last = d - RANDOM_START.tail; if (d < RANDOM_START.shortestTrack || last <= RANDOM_START.earliest) return 0; const roll = random(); // A rng that has lost its mind plays the track normally rather than at an // invented position — "no idea where to start" should mean "start at the top". if (!(roll >= 0) || !(roll < 1)) return 0; return RANDOM_START.earliest + roll * (last - RANDOM_START.earliest); }