DEV Community

Arelvox
Arelvox

Posted on Fully Autonomous

How Daily Puzzles Give Everyone the Same Answer Without a Server (Seeded RNG in JS)

Crack the Code, a daily number puzzle

Daily puzzles like Wordle have a neat trick: everyone in the world gets the same answer, yet there is no server deciding it. I used the same idea for Crack the Code, a daily number puzzle where you guess a secret 4-digit code in six tries. Here are the three pieces that make it work, all in plain JavaScript.

👉 Today's puzzle: arelvox.com/crack-the-code

1. Same code for everyone, no backend

The trick is a seeded random number generator. If everyone uses the same seed, everyone gets the same "random" numbers. The seed is just the day number:

var EPOCH = Date.UTC(2026, 9, 25);   // puzzle #1

function dayNum() {
  var n = new Date();
  var today = Date.UTC(n.getFullYear(), n.getMonth(), n.getDate());
  return Math.floor((today - EPOCH) / 86400000) + 1;
}
Enter fullscreen mode Exit fullscreen mode

Notice that it builds the date from the local year, month and day. That way the new puzzle arrives at midnight in your own time zone, not at midnight UTC.

For the generator itself, a tiny linear congruential generator is enough. It is not secure, but a puzzle doesn't need that:

function seeded(seed) {
  return function () {
    seed = (seed * 1664525 + 1013904223) % 4294967296;
    return seed / 4294967296;
  };
}

function makeCode(rnd, len) {
  var c = '';
  for (var i = 0; i < len; i++) c += Math.floor(rnd() * 10);
  return c;
}

var code = makeCode(seeded(dayNum() * 7919 + 12345), 4);
Enter fullscreen mode Exit fullscreen mode

Multiplying by a prime and adding an offset stops neighbouring days from producing similar first numbers. Math.random() could never do this, because you cannot seed it.

2. Scoring guesses when digits repeat

Codes can contain the same digit twice, like 0277. That is where a naive scorer breaks. If the code is 0277 and you guess 7777, how many yellows should you get? The answer is zero: two sevens are green, and there are no sevens left over for the other positions.

The fix is two passes. First mark the exact matches and count what is left over in the code. Then hand out yellows only from that leftover pool:

function score(guess, code) {
  var res = [], left = {}, i;

  // pass 1: greens, and count unmatched digits in the code
  for (i = 0; i < code.length; i++) {
    if (guess[i] === code[i]) res[i] = 'g';
    else left[code[i]] = (left[code[i]] || 0) + 1;
  }

  // pass 2: yellows, only while that digit is still available
  for (i = 0; i < code.length; i++) {
    if (res[i]) continue;
    if (left[guess[i]] > 0) { res[i] = 'y'; left[guess[i]]--; }
    else res[i] = 'x';
  }
  return res;
}
Enter fullscreen mode Exit fullscreen mode

This is the same algorithm Wordle uses for repeated letters.

3. Share the result without spoiling it

The share button turns your guesses into colored squares, so friends see how you did but not the code:

var map = { g: '🟩', y: '🟨', x: '⬛' };
var grid = results.map(function (r) {
  return r.map(function (c) { return map[c]; }).join('');
}).join('\n');

var text = 'Crack the Code #' + day + ' ' + tries + '/6\n' + grid;
navigator.share ? navigator.share({ text: text }) : navigator.clipboard.writeText(text);
Enter fullscreen mode Exit fullscreen mode

The problem: it was too easy

People finished the daily code almost every time, then left. Ten thousand possible codes sounds like a lot, but with six tries and good feedback, a sensible strategy (try 1234, then 5678) solves most days in four.

So I added a Time Attack mode for players who want more:

  • You start with 90 seconds. Each cracked code adds time, more if you used fewer tries.
  • A failed code costs 15 seconds.
  • Tries drop from 6 to 4 as you level up, and codes grow to 5 and then 6 digits.
  • Fast solves build a combo multiplier up to x5.
  • Every third code earns a power-up: reveal a digit, remove digits that aren't in the code, or freeze the clock.

The daily puzzle is still the hook, but the clock is what keeps people playing "just one more".

Try it

What's your best strategy for the first two guesses? Let me know in the comments, and see how many codes you can crack before the clock runs out.

Play Crack the Code →

There are more free browser games on the same site, including a one-tap stacking game: arelvox.com/games.

Top comments (0)