In some levels, the game shuffle the key in 2 jars. It happens in level 3-2, 4-2, 6-1, 6-2, 7-2 and 7-3.
From @The8bitbeast 's TAS (LINK), we learned that this RNG depends on the score and indeed it does! I've tried to look at the assembly code but it was simpler to tried out while playing.
The game keep the score in 0x00C2, for instance killing one enemy increase this score to 1, killing a boss increase to 48 and at the end of the level, the score increase on the time countdown (add 1 every 10)
When the 3rd digit of the score (from the right) is odd, the key will be on the right jar, and when it's even, it will be on the left.
the flag is set when the room is loaded, so either when scrolling up/down to the room or entering a door. exception for 7-3 where the room is loaded before entering the door.
for example here the score is "33400", the 3rd digit (4) is even so the key will be on the left.