← Nesim Apps

Same Seed, Same Level on Android and iOS in Kotlin Multiplatform

By Nesim Razon, independent mobile developer · 2026-11-23

#Algorithms#Level generation#iOS

My winter puzzle app shares one Kotlin codebase between Android and iOS, and its levels are generated, not stored. If the two platforms drew different random numbers from the same seed, a friend on an iPhone would see a different door puzzle than I do on Android. The fix is small: a 20-line random generator that lives in shared code. Here is how it works, how each level gets its seed, and how the result is verified and kept off the main thread.

The problem: one seed, two different levels

Generated levels only make sense if the seed decides everything. Level 14 of a sorting game must be the same puzzle for every player, on every device, today and after the next update. That is easy on one platform. It gets fragile with two.

Common Kotlin code cannot use java.util.Random at all, because that class exists only on the JVM. The default random source, Random.Default, cannot be seeded. Kotlin also has a seedable Random(seed), but I chose not to depend on a library detail for something that defines my levels. If an update ever changed its algorithm, every level in the app would silently change with it.

So the rule is: the generator is my own code, in the shared module, and nothing else may produce randomness for a level.

The generator: xorshift in about 20 lines

The class is called Rng and sits in the shared core package. It is xorshift64 with a multiplication, often written xorshift64*. State is one 64-bit Long. Each step does three shifts and xors, then multiplies:

Why this is safe on both platforms: a Kotlin Long is always 64 bits, overflow wraps around, and ushr is a logical shift. Android and iOS run the same arithmetic, so they produce the same sequence. Two small details protect it. A seed of 0 is replaced by the constant 0x2545F4914F6CDD1D, because an all-zero xorshift state never moves. And the constructor throws away the first four outputs, so that similar seeds do not start with similar numbers.

From a raw number to what a level needs

Everything else is built on nextLong. Each helper is one line you can follow by hand:

The remainder in nextInt has a tiny bias for large n. For a grid of a few dozen cells that is irrelevant, and it is the same bias on both platforms, which is the property that matters here.

One seed per level, and one per retry

Seeds are built by a second function, seedOf. It takes any parts, such as a game name and a level number, and folds every UTF-16 character of their text into a 64-bit hash with an xor and a multiplication by 1099511628211, starting from 1469598103934665603. It is a hash in the FNV style; I use it only to spread text and numbers into one Long, not for security.

A real call from the gift sorting game looks like this: Rng(seedOf("sort", id, attempt)). The game name keeps different games from sharing sequences. The id is the level. The attempt counter matters most: when a generated level is rejected, the next try uses attempt plus one, which is again a fixed seed. Retries are therefore deterministic too. Level 14 always ends on the same attempt, on every device.

The calendar part works the same way. Door puzzles in the app get their level id from the day: 1000 plus the day number for most doors of the season. Because the id is a plain Int, no locale, time zone or float formatting can change what goes into the hash.

Generate, then let a solver say yes

Random alone does not give playable levels. The generator makes a candidate and a solver checks it. For the sorting game, difficulty d from 1 to 10 gives colors = 3 + (d + 1) / 2, capped at 8, and the generator starts from a solved board and makes 25 + d * 14 reverse moves. So d = 1 means 4 colors and 39 shuffles, d = 5 means 6 colors and 95, d = 10 means 8 colors and 165.

A candidate is skipped if it ended up already solved, or if too many tubes are still sorted. Otherwise a best-first search with a limit of 60000 nodes must find a solution. If it does not, the loop continues with the next attempt. The ice sliding game does the same with up to 400 attempts and keeps the closest result if none lands in the wanted move range. The picture puzzle tries 300 symmetric patterns before it falls back to a mirrored one, and in every case a row solver must solve it completely.

The longer story of this check is in the article How Puzzle Levels Are Generated and Checked. The part that matters here is that the solver is also shared code, so both platforms accept and reject the same candidates.

Build it off the main thread

A search with up to 60000 nodes can take a noticeable time, and a code comment in the project says why it matters: on Kotlin/Native heavy levels must not freeze the main thread. All six game screens therefore use one small Compose function, rememberLevel(key, build). Inside it, produceState starts with null and sets the value from withContext(Dispatchers.Default) { build() }.

In practice a screen asks for its level like this: rememberLevel(req.levelId to req.difficulty) { GiftSort.generate(req.levelId, req.difficulty) }. The key tells Compose when to start again. While the value is null the screen shows a loading box, a small turning snowflake. When the value arrives, the game appears. Because generation is deterministic, running it on a background thread cannot change the result, only when it arrives.

Testing: same seed, same result

The repository has a test class, AllLevelsTest, that builds every level a player can reach: doors 1 to 31 and every free-play level, and asserts that each one has a solution. It runs as a host test on the JVM side, so it proves solvability, not equality between platforms.

For the equality itself the check is short. Create two generators with the same seed, draw 1000 numbers from each and compare the lists. Then generate the same level twice and compare the boards. A third check is worth adding when you have an iOS build: write the first 100 outputs for a few fixed seeds into the test as expected values, so any change to the generator, on any platform, breaks the build instead of changing levels quietly. I describe this as a pattern; the stored-values test is the one you would add, not one I can point to in the repo today.

Rules I follow: never pass Floats or locale-dependent text into a seed, never read the clock inside generation, and never add a second random source to a level.

Frequently asked questions

Why not use java.util.Random in shared Kotlin code?

It exists only on the JVM, so it is not available in common code that also targets iOS.

What is xorshift64*?

A small random generator with one 64-bit state. Each step uses three shift-and-xor operations and a final multiplication. It is fast and fully defined by integer arithmetic.

Why include the attempt number in the seed?

Rejected candidates are retried with a new seed, and keeping the attempt in the seed makes the retries deterministic, so a level always ends on the same attempt.

Why generate levels on Dispatchers.Default?

A solver search can take time. Running it in produceState with Dispatchers.Default keeps the screen responsive, and determinism guarantees the same result.

More from the blog