[Community Puzzle] Minesweeper level generator

R(1) = 139 % 6 is 1.
R(2) = 492 % 6 is 0 not 5.
This gets you a mine at (1,0)

Weird ok I guess I have a different bug somewhere, thanks.

Maybe you should mention that you need to take the modulus of the random number to get the X/Y values… It had me confused for a bit. This forum was empty a couple of days ago when I checked…

1 Like

Read the statement again :slight_smile:

Hi there, I get the first 3 mines on the easy level. 4th mine is first 4-4, which is occupied, then I get 2-5. Any idea where I could be off? I use python with the mod 2**32.

1 Like

@pieter-paul Here is the expected RNG in Python:

class RNG:
    def __init__(self, seed):
        self.s = seed
    def __call__(self):
        self.s = ((214013*self.s + 2531011) & 0xffffffff) >> 16
        return self.s

rand = RNG(31)
print([rand() for _ in range(5)]) # [139, 492, 1645, 5410, 17705]

(& 0xffffffff is equivalent to % 2**32)


Maybe statements should show more numbers for sample generation. That helps with overflow bugs check.


I’m getting the same values, 4-4, 2-5 then 4-2, 1-4 and so on. Maybe I’m missing something basic but could you explain how 4-4 is occupied?

Hello all,
I’m trying to make this challenge but my mines don’t get same position that example and i don’t know why.
First mines get is number 7 and last is 1, x is for the start of player :
. 7 . . 1 .
. . . . 2 .
. . . . . .
. . . x . 5
. 6 . . . .
. . 4 . 3 .
To generate mines, I reroll this until I have a valid position.
while (minesRemaining > 0) {
int x1 = nextValue(width);
int y1 = nextValue(height); }
My problem is here but how to generate position if it’s not that…
Thank you for helping me in advance :slight_smile:

@Lycast I think I had this result first as well, before I noticed that my calculation of the next 32-bit value of the RNG was not correct. I had an intermediate value that went beyond the 32-bit range. Maybe that helps?

Hello @MarcoT,
Thank you and yes I think that was it.
I am looking in JAVA how to respect this calculation.
Have a good day

unsigned int PRNG(unsigned int s){
return (214013 * s+ 2531011 / 65536);
does not give the correct numbers

PRNG(31) = 642077
Something is wrong, anyone has an idea ?

Maybe an overflow, try with unsigned long long.

@Razovsky The right parenthesis is misplaced, it should be (214013*s + 2531011) / 65536 (or equivalently >> 16).

Right, i’m dumb, thanks, i fixed it… I’m ashamed, blocked 2 hours by this, lol

I can’t have the same mine positions as in the testcases. The first mines are correct, but then they’re completely different, except for testcases “corner selection” and “beginner level”.
I generate numbers with this python code : RD = ((214013*RD+2531011)%2**32)//65536

[Community Puzzle] Minesweeper level generator - #21 by Niako may help you

I’m currently successful for all test cases except the expert level.

When placing mine # 77 it tries to place it at (x: 15, y: 7).
Which is wrong according to the example.

Even though I’ve magic-ed up the PRNG for uint32s (and it seems to work for all but the biggest test case), something still fails…

I’m using TypeScript. (Yes, not ideal for floats… I could choose a different language, but where’s the fun in that…)

function prng(seed: number): number {
    return Math.floor(((214013 * seed + 2531011) % 2**32) / 65536);

For every time I try to place a mine, the PRNG is called twice (with obviously the ‘previous’ result as seed).
If the result (x, y) is out of bounds, or it falls on a mine and/or starting 3x3, I just continue, ignoring BOTH previous results.

Someone has any ideas where to continue looking?


(x: 15, y: 7) are the good values the PRNG should get you when you try to place the #77, it can’t be placed there because the coordinates of the first selected cell is (x: 15, y: 8).

First thought was “no that can’t be, that code works… It’s tested in the other test cases…”
Well, of course there is 4 directions this could ‘fail’ in…

Thanks for pointing me in the right direction!

y >= yStart - 1 && 
y <= yStart + 1 && 
x >= xStart - 1 && 
x <= yStart + 1    // <--
1 Like