# [Community Puzzle] Minesweeper level generator

Coding Games and Programming Challenges to Code Better

Created by @darkhorse64,validated by @Westicles,@martinpapa69 and @DaNinja.
If you have any issues, feel free to ping them.

Hello,

For this challenge, I don’t understand how to find the coordinates x and y from the generator 32 bits.

I tried to use modulus w for x and modulus h for y but it didn’t match with the example.
Also I tried the formula for x = (nextR*w)//65535 and for y = (nextR’*h)//65535 and the values obtained are not again these ones in the example.

Can you give a hint for obtaining the pseudo randomized values (x,y) please ?

Confusion could have been avoided if the statement had these two info:

1. use mod w and mod h to get x and y
2. starts with rand_value to get x, y
4 Likes

Also apply mod 2**32 before dividing.
That’s what the point about unsigned long means.

2 Likes

java_coffee_cup is absolutely right. I have edited the statement accordingly.

3 Likes

Depending on the language, the mod 2**32 is not always necessary (for instance, all C flavors)

I am having hard time doing it in Java

``````    long lastValue=seed;
int nextWithMod(int modulo){
lastValue=((214013 * lastValue + 2531011) % 2^32) / 65536;
return Long.valueOf(lastValue % modulo).intValue();
}
``````

gives me 0 on every iteration.

Browsing Java docs, it looks like Integer is what you need.

@Antoniossss `^` is the bitwise xor, use `1L<<32` (1 left-shifted 32 times = binary 100…(32 times)…0 = 2³²) or directly `4294967296L` (final `L` for longs).

That is true

My first attempt to use long as 32uint was value & 0xFFFF_FFFF which in my mind was going to strip overflow bits over 32th bit and use that value. However, 0xFFFF_FFFF is an signed integer itself, thus last first big is beeing taken as sign anyway thus it was not working. What I had to do is use it as long so `0xFFFF_FFFFl` works as intended now.

@darkhorse64 using Integer would overflow into negatives.

OK thanks it’s easier to understand now I am getting the correct first five values in my program, but I am still confused how to use them . . . should I be able to get the first mine’s coordinates from R(1) % width and R(2) % height? Or am I going about this the wrong way?

1 Like

should I be able to get the first mine’s coordinates from R(1) % width and R(2) % height?

Have you tried it? What are the two numbers you got?

1 and 5, and (1,5) doesn’t line up with a mine on the solution grid

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

1 Like

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 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`)

3 Likes