Compare output from Keypad and random function

Something like below is wrong

char piny = PINb[0] * 1000 + PINb[1] * 100 + PINb[2] * 10 + PINb[3];

A char is a one byte variable and can contain values from 0 to 255 (or -128 to +127); it depends on interpretation and is (for now) outside the scope of the reply.

The result of your calculation will be truncated to a char. Change the type of piny to an int (the same type as pinx) and it can store a value up to 32768.